C#FileSystemWatcher复制文件夹完成 [英] C# FileSystemWatcher Copy folder complete

查看:362
本文介绍了C#FileSystemWatcher复制文件夹完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FileSystemWatcher监视将用于执行一些文件重命名的文件夹.
唯一要复制的是包含文件的文件夹.将没有单个文件放入受监视的文件夹.这是设置FileSystemWatcher的代码

I am using FileSystemWatcher to monitor a folder that will be used to do some file renaming.
The only thing that will be copied will be folders containing files. There will not be single files put into the monitored folder. This is the code for setting up the FileSystemWatcher

watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName;
watcher.IncludeSubdirectories = true;
watcher.Filter = "*.*";
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
watcher.EnableRaisingEvents = true; 

此设置似乎没有任何问题.

There doesn't seem to be any issues with this setup..

要复制的文件夹可以在50-200mb之间.在开始重命名过程之前,有没有办法检查/确保所有文件都已完成复制. 我尝试过这种想法,如果在调用GetFiles()时仍在进行复制,我将得到IOException.

The folders being copied can be between 50-200mb big. Is there a way to check/make sure that all the files have completed copying before starting the renaming process. I tried this thinking that i would get an IOException if the copying was still happening when the GetFiles() was called.

bool finishedCopying = false;
while (!finishedCopying)
{
    try
    {
        List<FileInfo> fileList = directoryInfo.GetFiles().ToList();
        AlbumSearch newAlbum = new AlbumSearch(directoryInfo);
        return newAlbum;
    }
    catch (IOException)
    {
        finishedCopying = false;
    }
}

如果需要更多信息,只需询问我可以提供的信息.

If anymore information is required, just ask an i can provide.

Ta.

推荐答案

我使用计时器对此进行了尝试.它可能不是目前最漂亮的解决方案,但在最初的测试中,到目前为止似乎还行得通.本质上,这是将文件夹复制到受监视的文件夹时,它将文件夹路径添加到AlbumList.该文件夹中的文件将触发Created事件.这等待文件完成复制.完成后,它将启动一个计时器.如果触发了新的Created事件,计时器将自行重置.

I gave this a go using a timer. It may not be the prettiest solution out there but at first testing it seems to be working so far. Essentially what this does is when a folder is copied to the monitored folder it will add the folder path to the AlbumList. The files in that folder will trigger the Created event. This waits for the file to finish copying. Once finished it starts a timer. If a new Created event gets triggered the timer will reset itself.

当timer.elapsed事件被触发时,它假定(并且我知道是所有f *& k ups之母)不再有要复制的文件,并且可以开始处理完全复制的文件夹.

When the timer.elapsed event is triggered it assumes (and I know assumption is the mother of all f*&k ups) that there are no more files to be copied and can start to process the fully copied folder..

System.Timers.Timer eventTimer = new System.Timers.Timer(); 
List<string> AlbumList = new List<string>();

private void watcher_Created(object sender, FileSystemEventArgs e)
{    
    if (Directory.Exists(e.FullPath))
    {
        AlbumList.Add(e.FullPath);
    }

    if (File.Exists(e.FullPath))
    {
        eventTimer.Stop();
        FileInfo newTrack = new FileInfo(e.FullPath);
        while (IsFileLocked(newTrack))
        {
            // File is locked. Do Nothing..
        }
        eventTimer.Start();              
    }
}

private void eventTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    List<string> ItemToRemove = new List<string>();
    foreach (var item in AlbumList)
    {            
        DirectoryInfo di = new DirectoryInfo(item);
        AlbumSearch newAlbum = new AlbumSearch(di);

        if (DoSomethingMethod(newAlbum))
        {
            ItemToRemove.Add(item);
        }
        else
        {
            // why did it fail
        }
    }

    foreach (var path in ItemToRemove)
    {
        AlbumList.Remove(path);
    }
}

private bool DoSomethingMethod(AlbumSearch as)
{
    // Do stuff here 
    return true;
}

这篇关于C#FileSystemWatcher复制文件夹完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆