我用C#编写了一个小应用程序,其中使用FileSystemWatcher监视特定文件夹。一旦文件被更新,它就会打开串口并将文件的内容写到串口中。但有时文件不会在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");
}
任何建议吗?