默认情况下,机器编写的脚本将文件保存到本地/服务器路径文件夹,但由于网络问题,两个文件夹不同步。我已经使用 FileSystemWatcher、DiffEngine、System.Timers 和 PingService 编写了一个 C# 窗口服务程序,如下面的编码来处理这个问题。要监视本地文件夹 OnChange 事件,在比较/复制到服务器路径之前 Ping 服务器 IP 是否成功/失败,当 Ping 失败时,它将转到 logtemp 文件夹,系统计时器处理此并在重新转储 logtemp 文件之前再次 Ping。
我不知道如何为此使用线程。ping 失败时,我的系统计时器代码应该在哪里?
protected override void OnStart(string[] args)
{ //all watcher config here//
watcher.Path = "path";
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "filename_company-Pg1_Product*";
watcher.Changed += new FileSystemEventHandler(LogFileSystemChanges);
watcher.EnableRaisingEvents = true;}
private void LogFileSystemChanges(object sender, FileSystemEventArgs e)
{
FileInfo sourcepath = new FileInfo(e.FullPath);
FileInfo destPath = new FileInfo(Path.Combine(dFile, e.Name));
FileInfo _tempPath = new FileInfo(Path.Combine(tempPath, e.Name));
if (PingService())
//PingService Bool Type....Ping Specific IP Before Compare/Copy
{
if (!destPath.Exists)
{
LogEvent(destPath + " DOES NOT EXIST!! ");
CopyFunction.CopyFile(sourcepath, destPath, true, true);
}
else
{
if (BinaryDiff(sFile, Path.Combine(dFile, e.Name)))
//DiffEngine If Source & Diff are Different is TRUE.
{
CopyFunction.CopyFile(sourcepath, destPath, true, true);
}
}
string msg = string.Format("Filename {0} are {1} now at {2} ", _
e.Name, e.ChangeType, DateTime.Now.ToString());
LogEvent(msg);
}
else
{
CopyFunction.CopyFile(sourcepath, _tempPath, true, true);
}
}