filewatchersystem在某段时间后没有触发事件

时间:2016-03-23 09:27:52

标签: c#

下面是我的代码,我正在运行Windows服务,但是如果我在那之后的5或6天长时间保持理想它会停止触发事件。虽然我的服务正在运行。请帮我这个

 private FileSystemWatcher watcher = new FileSystemWatcher();
 public bool StartFileWatcher()
        {
            _logger.Info("StartFileWatcher File watcher started");
            if (string.IsNullOrEmpty(_fileWatcherTargetPath))
            {
                _logger.Error("StartFileWatcher Directory name is null or Empty");
                return false;
            }
            DirectoryInfo dir = new DirectoryInfo(_fileWatcherTargetPath);
            if (!Directory.Exists(_fileWatcherTargetPath))
            {
                _logger.Info("StartFileWatcher Directory Created " + _fileWatcherTargetPath);
                Directory.CreateDirectory(_fileWatcherTargetPath);
            }

            //Add folder path to file watcher
            watcher.Path = _fileWatcherTargetPath;
            watcher.IncludeSubdirectories = true;

            //Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories.
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnChanged);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
            GC.KeepAlive(watcher);

            SetTimer();
        return true;
    }

0 个答案:

没有答案