如何确定文件夹是否已完成复制c#
作者:互联网
我有一个问题:如何确定文件夹是否已完成从一个位置复制到另一个位置?
目前,我的FileSystemWatcher会在复制目录中的文件后立即触发多个事件.我想要的是,当成功复制该文件夹中的所有文件时,将触发一个单独的事件.我的代码现在看起来像这样:
static void Main(string[] args)
{
String path = @"D:\Music";
FileSystemWatcher mWatcher = new FileSystemWatcher();
mWatcher.Path = path;
mWatcher.NotifyFilter = NotifyFilters.LastAccess;
mWatcher.NotifyFilter = mWatcher.NotifyFilter | NotifyFilters.LastWrite;
mWatcher.NotifyFilter = mWatcher.NotifyFilter | NotifyFilters.DirectoryName;
mWatcher.IncludeSubdirectories = true;
mWatcher.Created += new FileSystemEventHandler(mLastChange);
mWatcher.Changed += new FileSystemEventHandler(mLastChange);
mWatcher.EnableRaisingEvents = true;
Console.WriteLine("Watching path: " + path);
String exit;
while (true)
{
exit = Console.ReadLine();
if (exit == "exit")
break;
}
}
private static void mLastChange(Object sender, FileSystemEventArgs e)
{
Console.WriteLine(e.ChangeType + " " + e.FullPath);
}
解决方法:
不幸的是,FileSystemWatcher没有告诉你文件写完的时间.所以你的选择是……
>在假设没有更多更改到来时,在上次写入后设置超时
>让编写应用程序放置一个多种类型的锁定文件,告诉任何其他程序它已完成.
在重新阅读您的问题之后……听起来您对其他应用程序没有任何控制权.
因此,您需要某种超时值来确定何时完成所有写入操作.基本上创建一个计时器,在每个filesystemwatcher事件之后重置…当它超时时,你会触发表明它完成的单个事件.
以下是如何将其添加到代码中的方法……
static void Main(string[] args)
{
Timer.Interval = 5000; // 5 seconds - change to whatever is appropriate
Timer.AutoReset = false;
Timer.Elapsed += TimeoutDone;
String path = @"D:\Music";
FileSystemWatcher mWatcher = new FileSystemWatcher();
mWatcher.Path = path;
mWatcher.NotifyFilter = NotifyFilters.LastAccess;
mWatcher.NotifyFilter = mWatcher.NotifyFilter | NotifyFilters.LastWrite;
mWatcher.NotifyFilter = mWatcher.NotifyFilter | NotifyFilters.DirectoryName;
mWatcher.IncludeSubdirectories = true;
mWatcher.Created += new FileSystemEventHandler(mLastChange);
mWatcher.Changed += new FileSystemEventHandler(mLastChange);
mWatcher.EnableRaisingEvents = true;
Console.WriteLine("Watching path: " + path);
Timer.Start();
String exit;
while (true)
{
exit = Console.ReadLine();
if (exit == "exit")
break;
}
}
private static Timer Timer = new Timer();
private static void TimeoutDone(object source, ElapsedEventArgs e)
{
Console.WriteLine("Timer elapsed!");
}
private static void mLastChange(Object sender, FileSystemEventArgs e)
{
Console.WriteLine(e.ChangeType + " " + e.FullPath);
if (Timer != null)
{
Timer.Stop();
Timer.Start();
}
}
标签:c,directory,net,filesystemwatcher 来源: https://codeday.me/bug/20190703/1369004.html