Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use FileSystemWatcher on a single file in C#

When i try to set the watcher path to a single file like so:

watcher.Path = filePath1;

I get the error:

The directory name C:\Cromos 3.0\repository\diagnostics\dwm01_2011_06_13__09_03.LXD is invalid.

Can you only set the path to a folder directory?

like image 236
Jimmy Avatar asked Oct 09 '12 10:10

Jimmy


People also ask

Is FileSystemWatcher multithreaded?

Nope, filesystemwatchers run on their own thread.

What is FileSystemWatcher?

Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. You can create a component to watch files on a local computer, a network drive, or a remote computer.

What is the parameter that has to be used when a file is renamed after setting a FileSystemWatcher on the file?

If we want to notify the administrator, that a file has been changed, we would require the name of the file to which the change has been made. To get the name of the file we use the event handler argument FileSystemEventArgs.


Video Answer


2 Answers

Your error is setting the Path property with a full filename

watcher.Path = Path.GetDirectoryName(filePath1); 
watcher.Filter = Path.GetFileName(filePath1);

should work.

Not related to your proper question, but, of course, to enable the FileSystemWatcher's functionality, it is imperative to set the EnableRaisingEvents property to true.

like image 132
Steve Avatar answered Sep 29 '22 12:09

Steve


Yes, but you can watch for specific files by setting the filter property to the filename.

e.g.

watcher.Filter = "dwm01_2011_06_13__09_03.LXD";
like image 45
Justin Harvey Avatar answered Sep 29 '22 11:09

Justin Harvey