相关疑难解决方法(0)

FileSystemWatcher vs polling监视文件更改

我需要设置一个应用程序来监视在本地或网络驱动器上的目录中创建的文件.

请问FileSystemWatcher一个计时器或投票将是最好的选择.我过去曾使用过这两种方法,但并不广泛.

两种方法都存在哪些问题(性能,可靠性等)?

c# file-io filesystemwatcher distributed-filesystem

145
推荐指数
8
解决办法
7万
查看次数

为什么在Windows 7而不是Windows 8上检测到FileSystemWatcher属性更改?

我有一些代码使用FileSystemWatcher来监视我的应用程序之外的文件更改.

在Windows 7上,使用.NET 4,下面的代码将检测文件何时编辑并保存在记事本等应用程序中,同时我的应用程序正在运行.但是,这种逻辑在Windows 8上使用.NET 4无效.具体来说,FileSystemWatcher的Changed事件永远不会触发.

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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以改变NotifyFilter以包含NotifyFilters.LastWrite,它可以解决我的问题.但是,我想了解为什么此代码在Windows 7上有效但现在无法在Windows 8上触发Changed事件.我也很想知道在Windows 8中运行时是否有办法恢复我的Windows 7 FileSystemWatcher行为(不更改NotifyFilter).

.net c# windows-8

36
推荐指数
1
解决办法
4180
查看次数