我正在编写一个 C# 程序,让我知道何时添加或删除文件。我在我的 Windows 7 机器上运行它并观察我们网络上的 FTP 服务器。
它工作正常,但会突然停止捕获任何事件。我猜测它可能会失去与服务器的连接或网络出现故障。
如何在代码中处理这种情况。是否有一些异常我可以观察并尝试重新启动 FileSystemWatcher 对象。
任何建议和代码示例将不胜感激。
我需要为 FileSystemWatcher 添加错误处理程序
fileSystemWatcher.Error += new ErrorEventHandler(OnError);
然后添加此代码:
private void OnError(object source, ErrorEventArgs e)
{
if (e.GetException().GetType() == typeof(InternalBufferOverflowException))
{
txtResults.Text += "Error: File System Watcher internal buffer overflow at " + DateTime.Now + "\r\n";
}
else
{
txtResults.Text += "Error: Watched directory not accessible at " + DateTime.Now + "\r\n";
}
NotAccessibleError(fileSystemWatcher ,e);
}
以下是我重置 SystemFileWatcher 对象的方法:
static void NotAccessibleError(FileSystemWatcher source, ErrorEventArgs e)
{
source.EnableRaisingEvents = false;
int iMaxAttempts = 120;
int iTimeOut = 30000;
int i = 0;
while (source.EnableRaisingEvents == false && i < iMaxAttempts)
{
i += 1;
try
{
source.EnableRaisingEvents = true;
}
catch
{
source.EnableRaisingEvents = false;
System.Threading.Thread.Sleep(iTimeOut);
}
}
}
我认为这段代码应该做我想做的事。
之前的答案并没有完全解决这个问题,我不得不重置观察者而不仅仅是打开和关闭它。 我在 window 服务
上使用 filesystemwatchervoid NotAccessibleError(FileSystemWatcher source, ErrorEventArgs e)
{
int iMaxAttempts = 120;
int iTimeOut = 30000;
int i = 0;
while ((!Directory.Exists(source.Path) || source.EnableRaisingEvents == false) && i < iMaxAttempts)
{
i += 1;
try
{
source.EnableRaisingEvents = false;
if (!Directory.Exists(source.Path))
{
MyEventLog.WriteEntry("Directory Inaccessible " + source.Path + " at " + DateTime.Now.ToString("HH:mm:ss"));
System.Threading.Thread.Sleep(iTimeOut);
}
else
{
// ReInitialize the Component
source.Dispose();
source = null;
source = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(source)).BeginInit();
source.EnableRaisingEvents = true;
source.Filter = "*.tif";
source.Path = @"\\server\dir";
source.NotifyFilter = System.IO.NotifyFilters.FileName;
source.Created += new System.IO.FileSystemEventHandler(fswCatchImages_Changed);
source.Renamed += new System.IO.RenamedEventHandler(fswCatchImages_Renamed);
source.Error += new ErrorEventHandler(OnError);
((System.ComponentModel.ISupportInitialize)(source)).EndInit();
MyEventLog.WriteEntry("Try to Restart RaisingEvents Watcher at " + DateTime.Now.ToString("HH:mm:ss"));
}
}
catch (Exception error)
{
MyEventLog.WriteEntry("Error trying Restart Service " + error.StackTrace + " at " + DateTime.Now.ToString("HH:mm:ss"));
source.EnableRaisingEvents = false;
System.Threading.Thread.Sleep(iTimeOut);
}
}
}
您可以创建一个启动 FileSystemWatcher 的方法,如果出现错误,只需重新启动它即可。
private void WatchFile()
{
try
{
fsw = new FileSystemWatcher(path, filter)
{
EnableRaisingEvents = true
};
fsw.Changed += Fsw_Changed;
fsw.Error += Fsw_Error;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
private void Fsw_Error(object sender, ErrorEventArgs e)
{
Thread.Sleep(1000);
fsw.Changed -= Fsw_Changed;
fsw.Error -= Fsw_Error;
WatchFile();
}
我通过提高 (FileSystemWatcher 的名称).InternalBufferSize = MyInternalBufferSize 解决了这个问题 读取(FileSystemWatcher.InternalBufferSize 属性) 我使用最大 64 KB 的 InternalBufferSize 对其进行了测试,并在 FileSystemWatcher 路径中放置了 320 个虚拟文件,它像 Speedy Gonzales 一样运行。