作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 C# 编写了一个小型应用程序,其中涉及 Filesystemwather 以监视特定文件夹。一旦文件更新,它就会打开一个串行端口并将文件内容写入串行端口。但有时文件在这 4-5 小时内不会更新。文件更新后,filesystemwatcher 似乎进入休眠状态并且没有响应。
这是我的代码:
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"Z:\";
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.LastAccess;
watcher.Filter = "*.wsx";
watcher.Changed += new FileSystemEventHandler(OnChanged);
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
public static string CrL = "\r\n";
private static void OnChanged(object source, FileSystemEventArgs e)
{
string FileName;
FileName = e.FullPath;
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
Console.WriteLine("FILE is changed 1");
SerialPort port = new SerialPort("COM1");
port.Encoding = Encoding.ASCII;
port.Open();
using (System.IO.TextReader reader = System.IO.File.OpenText(e.FullPath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
port.Write(line);
port.Write(CrL);
}
}
port.Close();
Console.WriteLine("FILE is sent to com port");
}
关于这个的任何指示。
最佳答案
如果 dotnet 中存在导致 FileSystemWatcher“超时”的问题,按说:一个快速的解决方法是使用计时器控件并每隔一段时间重新初始化 FileSystemWatcher,这样它就不会“超时” ”。您可以访问 dotnet 调试符号服务器,因此您可以自己调试 FileSystemWatcher 以查看它的作用。
关于c# - 如何在 C# 中编写文件系统观察程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5800800/
我是一名优秀的程序员,十分优秀!