runtime icon indicating copy to clipboard operation
runtime copied to clipboard

FileSystemWatcher fails to trigger a file or Dir with a single char

Open WonderGnagno opened this issue 5 years ago • 5 comments

Hi, I noticed the below scenario

The class FileSystemWatcher fails to trigger event on a file/dir with a single char. The problem is related to OnCreated and OnChanged. Maybe ondelete as well but i didn't test it I ran the same code in with .NET 4.7 and it works correctly

I've Windows 10/1909 & .NET 5.0.101

Here below the class used to create the issue

 public class WatcherIssue
    {
        public void Start(string monitorPath)
        {
            if (!Directory.Exists(monitorPath)) throw new Exception($"Directory not found: {monitorPath}");
            // Create a new FileSystemWatcher and set its properties.
            using (FileSystemWatcher watcher = new FileSystemWatcher())
            {
                watcher.Path = monitorPath;

                // Watch for changes in LastAccess and LastWrite times, and
                // the renaming of files or directories.
                watcher.NotifyFilter = NotifyFilters.LastAccess
                                       | NotifyFilters.LastWrite
                                       | NotifyFilters.FileName
                                       | NotifyFilters.DirectoryName;

                // Only watch text files.
                watcher.Filter = "*";

                // Add event handlers.
                watcher.Created += OnCreated;
                watcher.Changed += OnCreated;

                // Begin watching.
                watcher.EnableRaisingEvents = true;
                watcher.IncludeSubdirectories = true;

                // Wait for the user to quit the program.
                Console.WriteLine("Press 'q' to quit the sample.");
                while (Console.Read() != 'q') ;
            }
        }
        private void OnCreated(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");
        }
    }

from a powershell i ran the below command:

Out-File -FilePath d this is not triggered
Out-File -FilePath dd this is triggered

Another test i did is related to the number of events triggered on a new file. Run:

Out-File -FilePath dd 

This operation trigger two events. Created and Changed is this a correct behavior?

BR

WonderGnagno avatar Dec 22 '20 22:12 WonderGnagno

Seems closely related to: https://github.com/dotnet/runtime/issues/45203

The difference is that this issue reports failure to trigger, the other one reports that name is null.

What is the behavior if you add a single-character file to a subfolder inside the watched root folder?

carlossanlop avatar Jan 08 '21 18:01 carlossanlop

Ping @WonderGnagno . Would you mind helping with this question?

What is the behavior if you add a single-character file to a subfolder inside the watched root folder?

carlossanlop avatar Jan 21 '21 18:01 carlossanlop

Hi Carlo, the power shell command i wrote explain the test. Let me know if somethings it's not clear.

Br Enrico

WonderGnagno avatar Jan 25 '21 11:01 WonderGnagno

@carlossanlop yeah, looks like one of security patches after 3.1.1 runtime break this one

Erfa1l avatar Mar 11 '21 21:03 Erfa1l

https://github.com/dotnet/runtime/issues/45203#issuecomment-885124518 this should be fixed on 3.1.9+ and 5+, let me know otherwise.

jozkee avatar Aug 21 '23 18:08 jozkee