C# follow folder junctions with FileSystemWatcher
Is it possible to configure a FileSystemWatcher
to watch other folders which are linked in with a开发者_运维知识库 folder junction point?
for example:
You are watching D:
You have D:\junction which points to E:\folder
When I create a file in E:\folder\file.txt I want to see it with the watcher as D:\junction\file.txt.
Is this possible?
Thanks.
FileSystemWatcher
is not supposed to monitor junctions or symlinks... and it monitors one folder at a time.
Although Junctions are not supported, I believe Hard Links are:
- http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw_19.html
- http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/fsutil_hardlink.mspx?mfr=true
A workaround is to setup FileSystemWatcher for each subfolder including junction points.
private static void SetupFileSystemWatchers(string path, FileSystemEventHandler changedEventHandler)
{
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
{
var watcher = new FileSystemWatcher(path);
watcher.IncludeSubdirectories = false;
watcher.Changed += changedEventHandler;
watcher.EnableRaisingEvents = true;
foreach (var subDirectory in Directory.GetDirectories(path))
{
SetupFileSystemWatchers(subDirectory, changedEventHandler);
}
}
精彩评论