3

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.

2
  • Is there no way to have it watch for a file to be created? Commented Mar 20, 2016 at 3:13
  • What's the problem to check directory for specified file creation event? Filter it by name or other params from all events. Commented Mar 20, 2016 at 3:18

1 Answer 1

3

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");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm having an issue with that code... I tried to use it: string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string path2 = desktopPath + @"\" + @"RaidTimestamps\"; FileSystemWatcher fw = new FileSystemWatcher(path2); fw.Created += fileSystemWatcher_Created; static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e) { MessageBox.Show(e.Name); if (e.Name == "orvmsnw1ss.log") { MessageBox.Show("Ok im here now"); } }
With the above, nothing is showing at all. Meaning the code isn't executing even though I know the path is 100% correct.
I did: MessageBox.Show(e.Name); And no, it didn't hit at all.
Oh my sweet jesus, you Grant, are a life saver.
@vivat pisces , my folder name location is c:\windows\system32\foldername , its giving exception:System.ArgumentException: The directory name 'C:\Windows\System32\foldername' does not exist . Its working fine from visual studio but gives exception from windows service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.