如何用 C# 编写文件系统观察程序?

发布于 2024-11-03 17:13:33 字数 1212 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎夕旧梦 2024-11-10 17:13:33

如果 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.

叹倦 2024-11-10 17:13:33

也许 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文