监视服务器重新启动后,FileSystemWatcher不会监视文件

时间:2018-07-02 13:01:58

标签: c# windows-services filesystemwatcher

我正在Windows服务中使用FileSystemWatcher监视不同服务器中的不同文件路径。

当任何监视服务器重新启动时,FileSystemWatcher不会监视该服务器的文件路径,而是监视其余未重新启动的服务器。

请指导我找出解决方案。任何帮助表示赞赏。

 private readonly FileSystemWatcher _fSWatcher;
 public FolderWatcher(string path, DataManager dataMgr, CancellationTokenSource tokenSrc)
        {
            try
            {
                _fSWatcher = new FileSystemWatcher();
                _fSWatcher.Path = @path;

                _cTokenSrc = tokenSrc;
                _cToken = tokenSrc.Token;

                _dataMgr = dataMgr;


                if (!_cToken.IsCancellationRequested)
                {
                    _fSWatcher.Renamed += _fSWatcher_Renamed;
                    _fSWatcher.Created += _fSWatcher_Created;
                    _fSWatcher.Deleted += _fSWatcher_Deleted;

                    _fSWatcher.IncludeSubdirectories = true;
                    _fSWatcher.EnableRaisingEvents = true;
                }
                else
                {
                    _fSWatcher.Dispose();
                }
            }
            catch (Exception ex)
            {
               // WriteErrorLog(ex.Message);
                _log.Error(ex.Message);
            }
        }

0 个答案:

没有答案