FileSystem观察器onChanged文件复制后

时间:2015-01-29 17:16:43

标签: c# filesystemwatcher

这段代码来自另一个堆栈答案,基本上我试图在用户文件夹中查找新文件,我知道OnCreate会在新文档完全被复制/下载之前触发&#34 ;,我试图做的是在每次onChange事件中复制它,但是复制似乎不会触发onChange一旦完成,尽管微软的文档说它呢?

对于记录onCreate工作正常。 waitingForClose声明为公共字符串列表,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问题,很快就会发布一个解决方案!

1 个答案:

答案 0 :(得分:0)

确定,

从它的外观看,FilesystemWatcher onChanged事件对于Windows 8.1最好是iffy(目前已经破解)。

我最终做的是从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));
    }