Why are FileSystemWatcher Attribute changes detected on Windows 7 but not Windows 8?(为什么在 Windows 7 而不是 Windows 8 上检测到 FileSystemWatcher 属性更改?)
问题描述
我有一些代码使用 FileSystemWatcher 来监视我的应用程序之外的文件更改.
I have some code that uses FileSystemWatcher to monitor file changes outside of my application.
在 Windows 7 上,使用 .NET 4,当我的应用程序运行时,以下代码将检测文件何时被编辑并保存在记事本等应用程序中.但是,此逻辑在 Windows 8 上使用 .NET 4 时不起作用.具体而言,FileSystemWatcher 的 Changed 事件永远不会触发.
On Windows 7, using .NET 4, the below code would detect when a file had been edited and saved in an application like Notepad, while my app was running. However, this logic isn't working using .NET 4 on Windows 8. Specifically, the FileSystemWatcher's Changed event never fires.
public static void Main(string[] args)
{
const string FilePath = @"C:userscraigdesktop
otes.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!");
}
}
我知道我可以将 NotifyFilter 更改为也包含 NotifyFilters.LastWrite,这可以解决我的问题.但是,我想了解为什么此代码在 Windows 7 上有效,但现在无法在 Windows 8 上触发 Changed 事件.我也很想知道在 Windows 8 中运行时是否有办法恢复我的 Windows 7 FileSystemWatcher 行为(不更改 NotifyFilter).
I understand that I can alter the NotifyFilter to also include NotifyFilters.LastWrite, which can solve my problem. However, I want to understand why this code worked on Windows 7 but now fails to fire the Changed event on Windows 8. I'm also curious to know if there's a way to restore my Windows 7 FileSystemWatcher behavior when running in Windows 8 (without changing the NotifyFilter).
推荐答案
到处都是评论太多,我只是添加一个答案来验证您是否意识到以下问题:
There are too many comments everywhere, I will just add an answer to verify that you are aware of the following issues:
- 获取文件系统观察器事件在 main 上发生UI 线程(寻找 Hans Passant 的答案)
- WPF - 无法更改 OnChanged 方法中的 GUI 属性(从 FileSystemWatcher 触发)
- get filesystemwatcher events to occur on main UI thread (look for Hans Passant's answer)
- WPF - Cannot change a GUI property inside the OnChanged method (fired from FileSystemWatcher)
显然问题在于该事件是在后台线程上引发的,您需要将调用编组回 UI 线程.
我在使用 FileSystemWatcher 类时遇到了很多麻烦,因此决定不使用它,正如您在此处所描述的那样:https://stackoverflow.com/a/22768610/129130.但是,我遇到的问题可能是由于线程同步问题和/或硬件问题造成的.
I have experienced a lot of trouble with the FileSystemWatcher class, and decided not to use it as you can see me describe here: https://stackoverflow.com/a/22768610/129130 . It is possible, however, that the problems I experienced were due to thread synchronization issues and / or hardware issues.
这篇关于为什么在 Windows 7 而不是 Windows 8 上检测到 FileSystemWatcher 属性更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么在 Windows 7 而不是 Windows 8 上检测到 FileSystemWatcher 属性更改?
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01