Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher ArgumentException

Tags:

c#

I'm having trouble understanding how FileSystemWatcher is supposed to work. I'm trying to get my code to wait for a file to exist, and then call on another function. My code is as follows:

string path2 = @"N:\reuther\TimeCheck\cavmsbayss.log";

        FileSystemWatcher fw = new FileSystemWatcher(path2);
       fw.Created += fileSystemWatcher_Created;

Then I have a seperate function that should handle the file once its event is called:

        static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
    {
        MessageBox.Show("Ok im here now");
    }

But it

The directory name N:\reuther\TimeCheck\cavmsbayss.log is invalid.

like image 314
Marshal Alessi Avatar asked Jan 23 '26 16:01

Marshal Alessi


1 Answers

According to the docs, the path parameter indicates:

The directory to monitor, in standard or Universal Naming Convention (UNC) notation.

Pass it the path to the directory, not the particular file:

string pathToMonitor = @"N:\reuther\TimeCheck";
FileSystemWatcher fw = new FileSystemWatcher(pathToMonitor);
fw.EnableRaisingEvents = true;  // the default is false, you may have to set this too
fw.Created += fileSystemWatcher_Created;

Then just watch out for the creation of that file, using either the Name or FullPath property in the FileSystemEventArgs class:

static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
    if (e.Name == "cavmsbayss.log")
    {
        MessageBox.Show("Ok im here now");
    }
}
like image 191
Grant Winney Avatar answered Jan 25 '26 05:01

Grant Winney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!