繁体   English   中英

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

[英]FileSystemWatcher as a Windows Service using Visual Studio 2019

所以,我创建了一个 Windows 服务,在那里我使用FileSystemWatcher来观察不同的目录。 每次检测到更改的文件时,我都会将它们复制到不同的目录中,以便以后可以使用它们。

当我将程序作为Console Application运行时,这非常有效

当我将它作为服务运行时,我可以正确启动和停止服务,但它不会检测到任何类型的事件。 我试图调试我的服务,我发现错误来自我没有停止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...

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM