none
c# FileSystemWatcher does not raise an event when a file is modified. It only raises the event when a file is created or deleted RRS feed

  • Question

  • I got a problem when i use c# FileSystemWatcher. It does not raise an Changed event when a file is modified. It only raises the event when a file is created or deleted.

    Note: i tested on windows 7 is ok, but window 10 is not.

    This is my code:

    public class MonitorSyncing
        {
            public MonitorSyncing(string monitorPath)
            {
                this._monitorPath = monitorPath;
            }

            private readonly string _monitorPath;

            public void Monitoring()
            {
                Console.WriteLine($"*** START MONITOR FOLDER: {this._monitorPath} ***");
                var fileSystemWatcherSyncFolderUserSettings = new FileSystemSafeWatcher
                {
                    Path = _monitorPath,
                    NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName |
                                   NotifyFilters.DirectoryName,
                    InternalBufferSize = 24576,
                };
                fileSystemWatcherSyncFolderUserSettings.Changed += OnChanged_SyncFolderUserSetting;
                fileSystemWatcherSyncFolderUserSettings.Created += OnChanged_SyncFolderUserSetting;
                fileSystemWatcherSyncFolderUserSettings.Deleted += OnChanged_SyncFolderUserSetting;
                fileSystemWatcherSyncFolderUserSettings.Renamed += OnRenamed_SyncFolderUserSetting;
                fileSystemWatcherSyncFolderUserSettings.Error += OnError;
                fileSystemWatcherSyncFolderUserSettings.IncludeSubdirectories = true;
                fileSystemWatcherSyncFolderUserSettings.EnableRaisingEvents = true;
            }

            private static void OnChanged_SyncFolderUserSetting(object source, FileSystemEventArgs e)
            {
                switch (e.ChangeType)
                {
                    case WatcherChangeTypes.Created:
                        Console.WriteLine($"Created file: {e.FullPath}");
                        break;
                    case WatcherChangeTypes.Deleted:
                        Console.WriteLine($"Deleted file: {e.FullPath}");
                        break;
                    case WatcherChangeTypes.Changed:
                        Console.WriteLine($"Change file: {e.FullPath}");
                        break;
                }
            }

            private static void OnRenamed_SyncFolderUserSetting(object source, RenamedEventArgs e)
            {
                //  Show that a file has been renamed.
                Console.WriteLine($"Renamed file '{e.OldFullPath}' to '{e.FullPath}'");
            }

            private static void OnError(object source, ErrorEventArgs e)
            {
                //  Show that an error has been detected.
                Console.WriteLine($"Error - The FileSystemWatcher has detected an error : {e.GetException().Message}");
                //  Give more information if the error is due to an internal buffer overflow.
                if (e.GetException().GetType() == typeof(InternalBufferOverflowException))
                {
                    Console.WriteLine(("Error - The file system watcher experienced an internal buffer overflow: " + e.GetException().Message));
                }
            }
        }

    Monday, September 25, 2017 8:35 AM

All replies

  • Just to be clear, this is entirely possible. It completely depends upon how the file is saved. As an example, notepad used to recreate the file each time. So you'd get a delete followed by a create. However larger files tend to require multiple writes so you may also get a create followed by several changes. It is completely dependent on how the writing program saves it. You have to handle them all. For watching for changes to a file you should be monitoring LastWrite and Size. Monitoring FileName and DirectoryName watches for renames, deletes and new files/folders. LastAccess does nothing as that isn't tracked in the file system anymore (at least by default).

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, September 25, 2017 2:03 PM
    Moderator
  • Hello Johnny,

    I have ran your code on windows 10 Enterprise and it works well.I'm not sure the way how to operate to modify file . is it just  change the file contents manually or create a stream by programming? And there is a circumstance that the change event doesn't fire. 

    As far as I know ,the FileSystemWatcher is just a wrapper around ReadDirectoryChangesW, and also you should consider it weather it's a BUGfrom win32 api .

    Sincerely,

    feih_7

    NOTE: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 26, 2017 7:09 AM
    Moderator
  • Hello Johnny,

    Is there any updates?

    Did you try to run the code in other pc, If the error still exists,you could give us more details about how to modify the file. Manual or Programming? and you could report the bug to there . because I test the code and work well in windows 10 Enterprise.

    Sincerely,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 6, 2017 6:43 AM
    Moderator