如何用 C# 编写文件系统观察程序?
我使用 C# 编写了一个小型应用程序,其中涉及 Filesystemwather 来监视特定文件夹。文件更新后,它会立即打开串行端口并将文件内容写入串行端口。但有时文件在 4-5 小时内都没有更新。文件更新后,文件系统观察器似乎进入睡眠状态并且没有响应。
这是我的代码:
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");
}
有关此代码的任何指针。
I have written a small application using C# which involves Filesystemwather to watch a particular folder. As soon as a file is updated it open up a serial port and writes the files contents to the serial port. But at times the file isn't updated for more this 4-5hours. And it seems like filesystemwatcher goes to sleep and doesn't respond after the file gets updated.
Here is my code:
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");
}
Any pointers on this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 dotnet 中存在导致 FileSystemWatcher “超时”的问题,可以这么说:快速解决方法是使用计时器控件并每隔一段时间重新初始化 FileSystemWatcher,这样它就不会“超时”。您可以访问 dotnet 调试符号服务器,因此您可以自己调试 FileSystemWatcher 以查看它的功能。
If there is an issue in dotnet that causes the FileSystemWatcher to "timeout", per say: a quick work-around would be to use a timer control and re-initialize the FileSystemWatcher every so often so it doesn't "timeout". You have access to the dotnet debug symbol servers so you could debug the FileSystemWatcher yourself to see what it does as well.
也许 FSW 对象正在被垃圾收集,因为它超出了范围。
在上面的代码中,您没有显示在哪里定义“观察者”对象。
如果它是在您使用它的相同方法中定义的,也许这就是问题所在。尝试在类的顶部定义它(在任何方法之外)。
Maybe the FSW object is being garbage-collected because it's going out of scope.
In your code above you haven't shown where you're defining the "watcher" object.
If it's defined in the same method where you use it, maybe that's the problem. Try defining it at the top of your class (outside of any methods).