文件复制后 FileSystemwatcher onChange
FileSystem watcher onChanged after file Copy
此代码来自另一个堆栈答案,本质上我正在尝试查看 Users 文件夹中的新文件,我知道 OnCreate 会在新文档完全完成之前触发 "copied/downloaded",我是什么我试图做的是在每个 onChange 事件中复制它,但是复制似乎不会在完成后触发 onChange,尽管 Microsoft 文档说它会触发?
郑重声明 onCreate 工作正常。 waitingForClose 被声明为 public 字符串列表,onCreate 添加文件很好。
private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
watcher.Path = @"C:\Users\MyUser";
watcher.IncludeSubdirectories = true;
watcher.Created += new FileSystemEventHandler(onCreate);
watcher.Changed += new FileSystemEventHandler(onChanged);
watcher.EnableRaisingEvents = true;
}
onCreate - 这会触发:
void onCreate(object sender, FileSystemEventArgs e)
{
var fileName = e.Name;
var destPath = @"C:\Users\myUser\Desktop\someFolder";
var destfile = System.IO.Path.Combine(destPath, fileName);
if (fileName.Substring(fileName.Length - 3, 3) == "doc" || fileName.Substring(fileName.Length - 3, 3) == "pdf" || fileName.Substring(fileName.Length - 4, 4) == "docx")
{
try
{
System.IO.File.Copy(e.FullPath, destfile, true);
}
catch
{
waitingForClose.Add(e.FullPath);
}
Console.WriteLine("File:" + e.FullPath + " " + e.ChangeType);
}
}
onChanged- 根本不会触发文件,即使在复制过程中也是如此:
void onChanged(object sender, FileSystemEventArgs e)
{
var fileName = e.Name;
var destPath = @"C:\Users\myUser\Desktop\someFolder";
var destfile = System.IO.Path.Combine(destPath, fileName);
if (fileName.Substring(fileName.Length - 3, 3) == "doc" || fileName.Substring(fileName.Length - 3, 3) == "pdf" || fileName.Substring(fileName.Length - 4, 4) == "docx")
{
if (waitingForClose.Contains(e.FullPath))
{
try
{
System.IO.File.Copy(e.FullPath, destfile, true);
waitingForClose.Remove(e.FullPath);
}
catch { }
}
}
}
好的,看起来这是一个 windows 8 问题,很快就会 post 解决!
好的,
从它的外观来看,FilesystemWatcher onChanged 事件充其量是不确定的(目前已损坏)Windows 8.1。
我最后做的是从 onCreate 创建一个文件数组,然后尝试每 5 秒从该数组复制一次:像这样:
void onCreate(object sender, FileSystemEventArgs e)
{
var fileName = e.Name;
var destPath = @"C:\Users\myuser\Desktop\Repo";
var destfile = System.IO.Path.Combine(destPath, fileName);
if (fileName.Substring(fileName.Length - 3, 3) == "doc" || fileName.Substring(fileName.Length - 3, 3) == "pdf" || fileName.Substring(fileName.Length - 4, 4) == "docx")
{
// We don't want to track the file if it's triggered a created event because it's copied to our repo.
if (!fileName.Contains("Repo"))
{
try
{
System.IO.File.Copy(e.FullPath, destfile, true);
}
catch
{
waitingForClose.Add(e.FullPath);
}
Console.WriteLine("File:" + e.FullPath + " " + e.ChangeType);
} // if (!fileName.Contains("repo"))
} // if (fileName.Substring(fileName.Length
} // void onCreate(
所以现在我们有一个文件数组,我们正在等待实际完成创建我在 Window_onLoad 事件中这样做:
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(onTick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();
现在开始 OnTick 事件
void onTick(object sender, EventArgs e)
{
List<String> tempList = new System.Collections.Generic.List<String>();
foreach(String item in waitingForClose){
try
{
System.IO.File.Copy(System.IO.Path.GetFullPath(item), @"C:\Users\myuser\Desktop\Repo\" + System.IO.Path.GetFileName(item), true);
tempList.Add(item);
}
catch {
// Do nothing
}
}
waitingForClose.RemoveAll(i => tempList.Contains(i));
}
此代码来自另一个堆栈答案,本质上我正在尝试查看 Users 文件夹中的新文件,我知道 OnCreate 会在新文档完全完成之前触发 "copied/downloaded",我是什么我试图做的是在每个 onChange 事件中复制它,但是复制似乎不会在完成后触发 onChange,尽管 Microsoft 文档说它会触发?
郑重声明 onCreate 工作正常。 waitingForClose 被声明为 public 字符串列表,onCreate 添加文件很好。
private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
watcher.Path = @"C:\Users\MyUser";
watcher.IncludeSubdirectories = true;
watcher.Created += new FileSystemEventHandler(onCreate);
watcher.Changed += new FileSystemEventHandler(onChanged);
watcher.EnableRaisingEvents = true;
}
onCreate - 这会触发:
void onCreate(object sender, FileSystemEventArgs e)
{
var fileName = e.Name;
var destPath = @"C:\Users\myUser\Desktop\someFolder";
var destfile = System.IO.Path.Combine(destPath, fileName);
if (fileName.Substring(fileName.Length - 3, 3) == "doc" || fileName.Substring(fileName.Length - 3, 3) == "pdf" || fileName.Substring(fileName.Length - 4, 4) == "docx")
{
try
{
System.IO.File.Copy(e.FullPath, destfile, true);
}
catch
{
waitingForClose.Add(e.FullPath);
}
Console.WriteLine("File:" + e.FullPath + " " + e.ChangeType);
}
}
onChanged- 根本不会触发文件,即使在复制过程中也是如此:
void onChanged(object sender, FileSystemEventArgs e)
{
var fileName = e.Name;
var destPath = @"C:\Users\myUser\Desktop\someFolder";
var destfile = System.IO.Path.Combine(destPath, fileName);
if (fileName.Substring(fileName.Length - 3, 3) == "doc" || fileName.Substring(fileName.Length - 3, 3) == "pdf" || fileName.Substring(fileName.Length - 4, 4) == "docx")
{
if (waitingForClose.Contains(e.FullPath))
{
try
{
System.IO.File.Copy(e.FullPath, destfile, true);
waitingForClose.Remove(e.FullPath);
}
catch { }
}
}
}
好的,看起来这是一个 windows 8 问题,很快就会 post 解决!
好的,
从它的外观来看,FilesystemWatcher onChanged 事件充其量是不确定的(目前已损坏)Windows 8.1。
我最后做的是从 onCreate 创建一个文件数组,然后尝试每 5 秒从该数组复制一次:像这样:
void onCreate(object sender, FileSystemEventArgs e)
{
var fileName = e.Name;
var destPath = @"C:\Users\myuser\Desktop\Repo";
var destfile = System.IO.Path.Combine(destPath, fileName);
if (fileName.Substring(fileName.Length - 3, 3) == "doc" || fileName.Substring(fileName.Length - 3, 3) == "pdf" || fileName.Substring(fileName.Length - 4, 4) == "docx")
{
// We don't want to track the file if it's triggered a created event because it's copied to our repo.
if (!fileName.Contains("Repo"))
{
try
{
System.IO.File.Copy(e.FullPath, destfile, true);
}
catch
{
waitingForClose.Add(e.FullPath);
}
Console.WriteLine("File:" + e.FullPath + " " + e.ChangeType);
} // if (!fileName.Contains("repo"))
} // if (fileName.Substring(fileName.Length
} // void onCreate(
所以现在我们有一个文件数组,我们正在等待实际完成创建我在 Window_onLoad 事件中这样做:
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(onTick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();
现在开始 OnTick 事件
void onTick(object sender, EventArgs e)
{
List<String> tempList = new System.Collections.Generic.List<String>();
foreach(String item in waitingForClose){
try
{
System.IO.File.Copy(System.IO.Path.GetFullPath(item), @"C:\Users\myuser\Desktop\Repo\" + System.IO.Path.GetFileName(item), true);
tempList.Add(item);
}
catch {
// Do nothing
}
}
waitingForClose.RemoveAll(i => tempList.Contains(i));
}