So far I know that FileSystemWatcher can look into a folder and if any of the files inside that folder is changed,modifies,.etc... then we can handle it. But I am not sure which filter and event I should use in my scenario: Watch for a Folder, If a file is added to that folder, do XYZ ... So In my scenario I don't care if an existing file is changed,etc..those should be ignored...only do XYZ if and only if a new file has been added to that Folder...
Which event and filter do you recommended for this scenario?
The FileSystemWatcher lets you detect several types of changes in a directory or file, like the 'LastWrite' date and time, changes in the size of files or directories etc. It also helps you detect if a file or directory is deleted, renamed or created.
File Watcher is an IntelliJ IDEA tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.
FileSystemWatcher.Created Event (System.IO) Occurs when a file or directory in the specified Path is created.
Set up the watcher:
FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = "Blah"; watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName; watcher.Created += new FileSystemEventHandler(OnChanged); watcher.EnableRaisingEvents = true;
Then implement the FileCreated
delegate:
private void OnChanged(object source, FileSystemEventArgs e) { Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); }
Please look here for a detailed explanation of the FileSystemWatcher: http://www.c-sharpcorner.com/uploadfile/mokhtarb2005/fswatchermb12052005063103am/fswatchermb.aspx
You will have to look for created files if you want to look for added files.
You specify the type of change to watch for by setting the value of a WatcherChangeType enumeration. The possible values are as follows:
Also you may just wire up the event handler that fires if a file is created (added) and not implement all the other events since they are not interesting for you:
watcher.Created += new FileSystemEventHandler(OnChanged);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With