1

I have written this code for watching files in my system, but its not alerting any modificationms in the folder or file. How can I achieve this? I am not understanding as it does not show any exceptions or errors.

static void Main(string[] args)
{
  FileSystemWatcher();
}

public static void FileSystemWatcher()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = @"D:\watcher";
    watcher.NotifyFilter = NotifyFilters.LastWrite;
    watcher.Filter = "*.*";
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;
    Console.Read();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
   Console.WriteLine(e.Name + " has changed");
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Abb
  • 3,051
  • 3
  • 17
  • 34
  • 4
    Are you sure the `Changed` event is the one you want? It will only monitor when files change, not when they are created, deleted or renamed. [Here's a list of events.](https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher_events(v=VS.110).aspx) – Dave Zych Sep 09 '15 at 14:32
  • @DaveZych Is it possible to achieve both, because I want only 1 XML file in the folder which will be changed from time to time. Here can I check wheteher I have a new extra xml file? – Abb Sep 09 '15 at 14:35
  • 1
    As @DaveZych suggested, there are more events. Add an event handler for the `Created` event to notify the user when a file has been created. – Melvin Sep 09 '15 at 14:37
  • Is the **D:** drive a local or network? – Black Frog Sep 09 '15 at 14:38
  • @Melvin The foremost thing to notice here is that my code is not notifying for a file content change. – Abb Sep 09 '15 at 14:39
  • @BlackFrog Its local. In future its goin to be client's machine – Abb Sep 09 '15 at 14:40
  • 1
    When you say future..client machine, does that mean you will be watching the drive/path via a network share? Or does it mean the executable will be running on client machine watching a local device? – Black Frog Sep 09 '15 at 14:42
  • I had a similar issue with a particular application - on updating a file it would make a copy of the original with a temporary name, change the temporary file, delete the original & the rename the temporary file to the original filename (I didn't write that application !!). The changed event did not fire in that situation - I traced that by adding handlers to all possible events to see what happened. – PaulF Sep 09 '15 at 14:42
  • @Black Frog Executable file – Abb Sep 09 '15 at 14:42

2 Answers2

3

I updated the code. The NotifyFilter needs to be expanded if you want to see new files added

    static void Main(string[] args)
    {
        FileSystemWatcher();
    }

    public static void FileSystemWatcher()
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"D:\temp";
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "*.*";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += Watcher_Created;
        watcher.Renamed += Watcher_Renamed;
        watcher.EnableRaisingEvents = true;
        Console.Read();
    }

    private static void Watcher_Renamed(object sender, RenamedEventArgs e)
    {
        Console.WriteLine(e.Name + " has been renamed");
    }

    private static void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine(e.Name + " has been added");
    }

    private static void OnChanged(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine(e.Name + " has changed");
    }
Joe
  • 221
  • 2
  • 11
  • How did you check? By changing content of the file? and if at what line of code? – Abb Sep 09 '15 at 14:42
  • 1
    Ran your code, opened a file in the temp directory and appended data to it. It notified me at once – Joe Sep 09 '15 at 14:43
  • Yes, it did work only when I open a file and change the content and save it. It only triggeres on saveing the file. Is there any way to check for a new file creating alert? – Abb Sep 09 '15 at 14:47
2

FileSystemWatcher.NotifyFilter Property

watcher.NotifyFilter <- is flag enum!

You need to write:

watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
       | NotifyFilters.FileName | NotifyFilters.DirectoryName;

...
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
MrDywar
  • 216
  • 2
  • 7
  • 2
    Thank you MrDywar, that is the correct answer. I updated my code example above @Abhishek – Joe Sep 09 '15 at 14:55