none
Why are FileSystemWatcher Attribute changes detected on Windows 7 but not Windows 8?

    Question

  • 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).

    Thanks,

    -Craig

    Tuesday, November 20, 2012 11:31 PM

Answers

All replies

  • Hi Craig,

    Welcome to the MSDN Forum.

    I reproduced this scenario.

    I am trying to involving some other one into this thread, wait it patiently, please.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, November 21, 2012 12:34 PM
    Moderator
  • Hi Craig,

    I can reproduce this issue and reported it on the Connect site.

    https://connect.microsoft.com/VisualStudio/feedback/details/772182/msdn-forum-why-are-filesystemwatcher-attribute-changes-detected-on-windows-7-but-not-windows-8

    Please use the above link to further monitor the status of this issue.

    Have a nice day!


    Regards,
    Christian HL
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Monday, November 26, 2012 6:36 AM
  • Any update on this? Our applications would not run because of this issue and is a major problem. Is there a solution or workaround in Windows 8?

    Thanks

    Thursday, November 29, 2012 10:14 PM
  • Hi BRLHer,

    The workaround is using correct NotifyFilter value.

    http://msdn.microsoft.com/en-us/library/system.io.notifyfilters.aspx

    For example, using NotifyFilter.LastWrite if you want to capture if the file or folder last had anything written to it.


    Regards,
    Christian HL
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Marked as answer by Mike FengModerator Friday, November 30, 2012 3:08 AM
    • Unmarked as answer by clichten Tuesday, December 04, 2012 7:47 PM
    Friday, November 30, 2012 1:53 AM
  • I tried your suggestion of incorporating LastWrite but there are scenarios where it still doesn't work. For instance, let's say I'm using Subversion source control and I revert an edited file directly in Explorer. On Windows 7, my application would detect the revert occurred via the Attributes notify filter and properly notify me. On Windows 8, this doesn't work, as reported above. However, using the LastWrite filter fails as well, despite the file's contents being changed on the revert.

    Also, your replies failed to answer the actual question I asked in the original post. Specifically, I want to know what changed in Windows 8 that broke this behavior.

    Thanks,

    -Craig

    Tuesday, December 04, 2012 7:52 PM