Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSystemWatcher doesn't notice watched directory rename #13753

Closed
matthew-a-thomas opened this issue Nov 7, 2019 · 2 comments
Closed

FileSystemWatcher doesn't notice watched directory rename #13753

matthew-a-thomas opened this issue Nov 7, 2019 · 2 comments

Comments

@matthew-a-thomas
Copy link

FileSystemWatcher doesn't notice when its watched directory is renamed.

.Net Core 3.0
Windows 10 x64 1903 build 18362.10024

Code that reproduces the issue:

var path = Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(path);
using var watcher = new FileSystemWatcher(path)
{
    EnableRaisingEvents = true
};
watcher.Changed += (o, e) => Console.WriteLine("Changed " + e.FullPath);
watcher.Created += (o, e) => Console.WriteLine("Created " + e.FullPath);
watcher.Deleted += (o, e) => Console.WriteLine("Deleted");
watcher.Error += (o, e) => Console.WriteLine("Error");
watcher.Renamed += (o, e) => Console.WriteLine("Renamed");
            
var movedPath = path + " moved";
Directory.Move(path, movedPath); // Does not raise any events
File.WriteAllText(Path.Combine(movedPath, "file.txt"), "Hello, world!"); // Raises event with incorrect FullPath (has old directory, not new one)
Thread.Sleep(-1);
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@JeremyKuhne
Copy link
Member

This has always been the behavior and is an artifact of the underlying Win32 API- https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw. You need to watch the parent directory. You can mitigate cost by not watching recursively and adding as many filters as possible.

@JeremyKuhne JeremyKuhne removed the untriaged New issue has not been triaged by the area owner label Mar 3, 2020
@matthew-a-thomas
Copy link
Author

This has always been the behavior and is an artifact of the underlying Win32 API- https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw. You need to watch the parent directory. You can mitigate cost by not watching recursively and adding as many filters as possible.

@JeremyKuhne Even so, the problem remains that e.FullPath contains an incorrect path as shown in my example. IMHO the API is broken: it promises to give the correct full path and it doesn't but gives non-existent paths. Apparently the only portion of that property's string that is guaranteed to be correct is the very last path segment, but that's not a "full" path in any sense.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants