Everywhere I find these two lines of code used to set filter for file system watcher in samples provided..
FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Filter = "*.txt"; //or watcher.Filter = "*.*";
But I want my watcher to monitor more file types, but not all. How can I achieve this:
//watcher.Filter = "*.txt" | "*.doc" | "*.docx" | "*.xls" | "*.xlsx";
I tried these:
watcher.Filter = "*.txt|*.doc|*.docx|*.xls|*.xlsx"; // and watcher.Filter = "*.txt;*.doc;*.docx;*.xls;*.xlsx*";
Both did not work. This is just basics but I miss it. Thanks..
Nope, filesystemwatchers run on their own thread.
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.
Note that a FileSystemWatcher may miss an event when the buffer size is exceeded. To avoid missing events, follow these guidelines: Increase the buffer size by setting the InternalBufferSize property. Avoid watching files with long file names, because a long file name contributes to filling up the buffer.
The Filter property can be changed after the FileSystemWatcher object has started receiving events. For more information about filtering out unwanted notifications, see the NotifyFilter, IncludeSubdirectories, and InternalBufferSize properties. Filter accepts wildcards for matching files, as shown in the following examples.
It is normal for FileSystemWatcher to raise multiple events for file copies. Generally at least one create, and one change event is raised, plus there might be more. If an antivirus program is running there can be even more. 10 is unusual, but I don't see any issue with your code which only raises three events per file on my system.
The Filter property only supports one filter at a time. From the documentation: Use of multiple filters such as *.txt|*.doc is not supported. You need to create a FileSystemWatcher for each file type. You can then bind them all to the same set of FileSystemEventHandler:
File System Watcher. Filter Property System. IO Gets or sets the filter string used to determine what files are monitored in a directory. The filter string. The default is "*.*" (Watches all files.) The following example creates a FileSystemWatcher to watch the directory specified at run time.
You can't do that. The Filter
property only supports one filter at a time. From the documentation:
Use of multiple filters such as
*.txt|*.doc
is not supported.
You need to create a FileSystemWatcher
for each file type. You can then bind them all to the same set of FileSystemEventHandler
:
string[] filters = { "*.txt", "*.doc", "*.docx", "*.xls", "*.xlsx" }; List<FileSystemWatcher> watchers = new List<FileSystemWatcher>(); foreach(string f in filters) { FileSystemWatcher w = new FileSystemWatcher(); w.Filter = f; w.Changed += MyChangedHandler; watchers.Add(w); }
There is a workaround.
The idea is to watch for all extensions and then in the OnChange event, filter out to desired extensions:
FileSystemWatcher objWatcher = new FileSystemWatcher(); objWatcher.Filter = "*.*"; objWatcher.Changed += new FileSystemEventHandler(OnChanged); private static void OnChanged(object source, FileSystemEventArgs e) { // get the file's extension string strFileExt = getFileExt(e.FullPath); // filter file types if (Regex.IsMatch(strFileExt, @"\.txt)|\.doc", RegexOptions.IgnoreCase)) { Console.WriteLine("watched file type changed."); } }
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