37
votes

I have some code that uses FileSystemWatcher to monitor file changes outside of my application.

On Windows 7, using .NET 4, the below code would detect when a file had been edited and saved in an application like Notepad, while my app was running. However, this logic isn't working using .NET 4 on Windows 8. Specifically, the FileSystemWatcher's Changed event never fires.

public static void Main(string[] args)
{
    const string FilePath = @"C:\users\craig\desktop\notes.txt";

    if (File.Exists(FilePath))
    {
        Console.WriteLine("Test file exists.");
    }

    var fsw = new FileSystemWatcher();
    fsw.NotifyFilter = NotifyFilters.Attributes;
    fsw.Path = Path.GetDirectoryName(FilePath);
    fsw.Filter = Path.GetFileName(FilePath);

    fsw.Changed += OnFileChanged;
    fsw.EnableRaisingEvents = true;

    // Block exiting.
    Console.ReadLine();
}

private static void OnFileChanged(object sender, FileSystemEventArgs e)
{
    if (File.Exists(e.FullPath))
    {
        Console.WriteLine("File change reported!");
    }
}

I understand that I can alter the NotifyFilter to also include NotifyFilters.LastWrite, which can solve my problem. However, I want to understand why this code worked on Windows 7 but now fails to fire the Changed event on Windows 8. I'm also curious to know if there's a way to restore my Windows 7 FileSystemWatcher behavior when running in Windows 8 (without changing the NotifyFilter).

5
Have you been following Microsofts Online Support Suggestions posted on this web site I assume that you are that same Craig..social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/…MethodMan
Pure the documentation located here: msdn.microsoft.com/en-us/library/… it indicates it should work in Windows 8. Does the file in question even have an attributes? This appears to be a bug. For the time being you will have to use a workaround until its fixed.Security Hound
there are a lot of things that should work Per "THERE DOCUMENTATION" but there are bugs even in .NET 4.0 that's broken working things that were in 3.5 and 2.0MethodMan
@DJKRAZE - I posted that commented as you posted your commented and linked to a thread which indicated an actual bug report was filed. They have only been looking at the bug report since 11/25/2012 its going to take awhile.Security Hound
@DJKRAZE Yes, I am the same Craig. I posted here since the MSDN forum admin simply added my issue to Connect. While that's nice, Ramhound is correct that I won't be hearing back from MS for quite awhile. Further, I was hoping someone on StackOverflow could answer my actual question, which is why there's different behavior on Windows 8, not necessarily how to fix it.Craig

5 Answers

1
votes

Check the archive bit on the file before/after you edit it. Your code is only searching for Attributes changes, so my guess is that Windows 7 is updating the Archive bit on the file, and windows 8 is not.

1
votes

There are too many comments everywhere, I will just add an answer to verify that you are aware of the following issues:

Apparently the problem is that the event is raised on a background thread, and you need to marshal the call back to the UI thread.

I have experienced a lot of trouble with the FileSystemWatcher class, and decided not to use it as you can see me describe here: https://stackoverflow.com/a/22768610/129130 . It is possible, however, that the problems I experienced were due to thread synchronization issues and / or hardware issues.

0
votes

FileSystemWatcher is notoriously unreliable. Try subscribing to all the events and see if the others fire. One thing that you could try is to use a timer to examine the file for changes at regular intervals, say once every two seconds, instead of using FileSystemWatcher.

0
votes

I had the same problem. This class seems to work on my windows 8 computer:

https://stackoverflow.com/a/23743268/637142

Reason why I use that class is because it behaves the same on windows 7 and windows 8.

-1
votes

I don't know why but I find that under Windows 8.1

NotifyFilters.LastWrite (Changed event) of class FileSystemWatcher will fire

  • if I monitor directory inside my desktop(C:\Users\[user]\Desktop).

the event will not fire

  • if I monitor program files directory (C:\Program Files (x86))

May be related to permission but I don't know how to config it, both condition are run under administrator