微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

FileSystemWatcher 作为使用 Visual Studio 2019 的 Windows 服务

如何解决FileSystemWatcher 作为使用 Visual Studio 2019 的 Windows 服务

所以,我创建了一个 Windows 服务,我在其中使用 FileSystemWatcher 来查看不同的目录。每次检测到更改的文件时,我都会将它们复制到不同的目录中,以便稍后处理。

当我将程序作为控制台应用程序运行时,这完美

当我将它作为服务运行时,我可以正确地启动和停止服务,但它不会检测到任何类型的事件。 我试图调试我的服务,但我发现错误来自于我没有停止 FileSystemWatcher 的事实。

对于我的控制台应用程序,我有 Watch() 方法代码

    public void Watch()
    {
        using (FileSystemWatcher watcher = new FileSystemWatcher($"C:\\Users\\wost\\AppData\\Roaming\\Sublime",_ext))
        {
            watcher.NotifyFilter = NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.FileName
                                 | NotifyFilters.DirectoryName;

            watcher.IncludeSubdirectories = true;
            // Add event handlers.
            watcher.Changed += OnChanged;
            watcher.Created += OnChanged;
            watcher.Deleted += OnChanged;
            watcher.Renamed += OnRenamed;

            // Begin watching.
            watcher.EnableRaisingEvents = true;

            // Wait for the user to quit the program.
            Console.WriteLine("Press 'q' to quit the sample.");
            while (Console.Read() != 'q') ;
        }
    }

所以,如果用户按下“q”,我会停止程序。

对于我的 Windows 服务,我有 Watch() 方法代码

 public void Watch()
    {
        using (FileSystemWatcher watcher = new FileSystemWatcher($"C:\\Users\\lashi\\AppData\\Roaming\\Sublime Text 3",_ext))
        {
            watcher.NotifyFilter = NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.FileName
                                 | NotifyFilters.DirectoryName;

            watcher.IncludeSubdirectories = true;
            // Add event handlers.
            watcher.Changed += OnChanged;
            watcher.Created += OnChanged;
            watcher.Deleted += OnChanged;
            watcher.Renamed += OnRenamed;

            // Begin watching.
            watcher.EnableRaisingEvents = true;

        }
    }

所以,在这里我根本没有停止 FileSystemWatcher,因为我没有与用户直接交互,我不知道如何停止它。你能帮我找到解决办法吗?

这些是 OnStart()OnStop() 方法

public partial class Service1 : ServiceBase
{
    Watcher w;
    
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart()
    {
        w = new Watcher($"C:\\emmC_CACHE\\expt1-log",$"C:\\emmC_CACHE\\expt1-file","lashi");
        w.Watch();
    }

    protected override void OnStop()
    {
        DirectoryInfo dir = new DirectoryInfo(@"C:\Users\wost\Desktop\FILES");
        int count = dir.GetFiles().Length;
        // TEST
        if (count == 0)
        {
            StreamWriter writer = new StreamWriter(@"C:\Users\wost\Desktop\Notes.txt");
            writer.WriteLine("Service is stopped at: " + DateTime.Now);
            writer.Close();
        }
    }
}

解决方法

我认为你需要让观察者成为一个领域,而不是过早地处置它。我没有测试这段代码,但你会在“观察”中看到相关的变化,我想......

internal class Watcher : IDisposable {

    private FileSystemWatcher _watcher;
    private string _directoryPath;  
    private string _ext;
    
    internal Watcher (string directoryPath,string ext) {
            _directoryPath = directoryPath;
            _ext = ext;
    }
    ~Watcher () {
        Dispose();
    }
    public void Watch()
    {
        Dispose();
        
        _watcher = new FileSystemWatcher(_directoryPath,_ext);
        
        _watcher.NotifyFilter = NotifyFilters.LastAccess
                             | NotifyFilters.LastWrite
                             | NotifyFilters.FileName
                             | NotifyFilters.DirectoryName;

        _watcher.IncludeSubdirectories = true;
        // Add event handlers.
        _watcher.Changed += OnChanged;
        _watcher.Created += OnChanged;
        _watcher.Deleted += OnChanged;
        _watcher.Renamed += OnRenamed;

        // Begin watching.
        _watcher.EnableRaisingEvents = true;
    }
    
     public void Dispose() {
        try {
           _watcher?.Dispose();
        } catch {
           ;
        }
        _watcher = null;
     }
     //FSW event handlers...

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。