|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace FileFlit
{
public class Monitor
{
protected List<FileSystemWatcher> watchList;
protected static Monitor monitor;
public static void Go(List<FlitterRecord> fileList)
{
if (monitor == null)
{
monitor = new Monitor();
}
monitor.WatchFiles(fileList);
}
public static void UpdateNow(List<FlitterRecord> fileList)
{
foreach (FlitterRecord fr in fileList)
{
try
{
File.Copy(fr.SourceFile, Path.Combine(fr.DestinationFolder, Path.GetFileName(fr.SourceFile)), true);
Form1.Log(true, fr);
}
catch (Exception e)
{
Form1.Log(false, fr, e.Message);
}
}
}
public Monitor()
{
watchList = new List<FileSystemWatcher>();
}
protected void WatchFiles(List<FlitterRecord> fileList)
{
foreach (FileSystemWatcher fsw in watchList)
{
fsw.Changed -= OnChanged;
}
List<FileSystemWatcher> newWatchList = new List<FileSystemWatcher>();
foreach (FlitterRecord fr in fileList)
{
FlitterFileSystemWatcher fsw = new FlitterFileSystemWatcher(Path.GetDirectoryName(fr.SourceFile), Path.GetFileName(fr.SourceFile)) { FlitterRecord = fr };
fsw.NotifyFilter = NotifyFilters.LastWrite;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.EnableRaisingEvents = true;
newWatchList.Add(fsw);
}
watchList = newWatchList;
}
protected void OnChanged(object sender, FileSystemEventArgs e)
{
FlitterRecord fr = ((FlitterFileSystemWatcher)sender).FlitterRecord;
bool success=false;
int retries = 3;
while (!success)
{
try
{
Thread.Sleep(fr.Delay);
File.Copy(fr.SourceFile, Path.Combine(fr.DestinationFolder, Path.GetFileName(fr.SourceFile)), true);
success = true;
}
catch(Exception ex)
{
if (--retries == 0)
{
Form1.Log(false, fr, ex.Message);
break;
}
}
if (success)
{
Form1.Log(true, fr);
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.