Reputation: 105
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.
Upvotes: 3
Views: 1348
Reputation: 66449
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");
}
}
Upvotes: 3