Private static void OnRenamed(object source, RenamedEventArgs e) Specify what is done when a file is changed, created, or deleted.Ĭonsole.WriteLine("File: " e.FullPath " " e.ChangeType) įile.Copy(e.FullPath, Studio 2012\Projects\Generac_fileWatcher\Generac_fileWatcher\SyncedDirectory\whatever.txt", true) Private static void OnChanged(object source, FileSystemEventArgs e) Wait for the user to quit the program.Ĭonsole.WriteLine("Press \'q\' to quit the sample.") Watcher.Renamed = new RenamedEventHandler(OnRenamed) Watcher.Deleted = new FileSystemEventHandler(OnChanged) Watcher.Created = new FileSystemEventHandler(OnChanged) Watcher.Changed = new FileSystemEventHandler(OnChanged) | NotifyFilters.FileName | NotifyFilters.DirectoryName Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite * Watch for changes in LastAccess and LastWrite times, and Watcher.Path = studio 2012\Projects\Generac_fileWatcher\Generac_fileWatcher\FilesToWatch" Create a new FileSystemWatcher and set its properties.įileSystemWatcher watcher = new FileSystemWatcher() Console.WriteLine("Usage: Watcher.exe (directory)") // Display the proper way to call the program. // If a directory is not specified, exit program. I've pasted my code below, and welcome any thoughts. bmp file, the destination directory will add a "whatever.txt" file by default. My other challenge is to reflect the file that is being created in the destination directory. It seems I can only get one file to create in the destination directory, and any other new files created are over written. My issue is that I'm trying to setup my File Watcher program so that when new files are added, they are not over written in the destination directory but added. I've read a lot of material on the Microsoft site, and Stack Overflow too, but not finding a solution. Note: building for Linux 圆4 single file self-contained exe.Hello Stack Overflow community, I'm still learning the basics of c# and am need of some guidance. The delegate still doesn't trigger when running the built exe in a Linux environment. _logger.LogError($"Couldn't watch folder: ") Īttempting to use the answer in this related SO quetion: did not fix the issue. Watcher.NotifyFilter = NotifyFilters.Attributes Using var watcher = new FileSystemWatcher(directoryPath) Throw new Exception("Folder location was not set.") If (string.IsNullOrWhiteSpace(directoryPath)) NET 5 was meant to work "portably."Īny tips/details would be appreciated as I'm sure others have come across this as well, or are at least likely to.Ĭurrent code that works with Windows build but not Linux: public void WatchFolder() NET 5 shouldn't be used for Linux/Mac/Unix builds? I can't find any definitive docs on non-Windows usage, but thought that. The watcher apparently sets up just fine (code executes and console/logging messages are printed out) but none of the file system events are ever triggered, ie: Change/Create/Delete/Rename/etc nothing triggers those.Īre there any undocumented or "hidden" setup details needed to work with Linux, or is this not supported altogether and even though it's in. The problem though, I'm targeting Ubtuntu 18.04 lts and greater for running this. Based heavily off of the example on the MS docs which works fine on my windows machine. I'm trying to watch for changes in a specified directory using the FileSystemWatcher class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |