等待文件完全写入

在一个目录中创建(FileSystemWatcher_Created)文件后,我将其复制到另一个目录。但是,当我创建一个大的(> 10MB)文件,它无法复制的文件,因为它启动复制已经,当文件尚未完成创建... 

这会导致无法复制的文件,因为它是由另一个进程使用是提高。;(有

帮助吗?


class Program

{

    static void Main(string[] args)

    {

        string path = @"D:\levan\FolderListenerTest\ListenedFolder";

        FileSystemWatcher listener; 

        listener = new FileSystemWatcher(path);

        listener.Created += new FileSystemEventHandler(listener_Created);

        listener.EnableRaisingEvents = true;


        while (Console.ReadLine() != "exit") ;

    }


    public static void listener_Created(object sender, FileSystemEventArgs e)

    {

        Console.WriteLine

                (

                    "File Created:\n"

                   + "ChangeType: " + e.ChangeType

                   + "\nName: " + e.Name

                   + "\nFullPath: " + e.FullPath

                );

        File.Copy(e.FullPath, @"D:\levan\FolderListenerTest\CopiedFilesFolder\" + e.Name);

        Console.Read();

    }

}


一只斗牛犬
浏览 615回答 3
3回答

Helenr

对于您面临的问题,只有解决方法。开始复制之前,请检查文件ID是否正在处理中。您可以调用以下函数,直到获得False值为止。第一种方法,直接从此答案复制:private bool IsFileLocked(FileInfo file){&nbsp; &nbsp; FileStream stream = null;&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);&nbsp; &nbsp; }&nbsp; &nbsp; catch (IOException)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //the file is unavailable because it is:&nbsp; &nbsp; &nbsp; &nbsp; //still being written to&nbsp; &nbsp; &nbsp; &nbsp; //or being processed by another thread&nbsp; &nbsp; &nbsp; &nbsp; //or does not exist (has already been processed)&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; finally&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (stream != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.Close();&nbsp; &nbsp; }&nbsp; &nbsp; //file is not locked&nbsp; &nbsp; return false;}第二种方法:const int ERROR_SHARING_VIOLATION = 32;const int ERROR_LOCK_VIOLATION = 33;private bool IsFileLocked(string file){&nbsp; &nbsp; //check that problem is not in destination file&nbsp; &nbsp; if (File.Exists(file) == true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; FileStream stream = null;&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex2)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //_log.WriteLog(ex2, "Error in checking whether file is locked " + file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int errorCode = Marshal.GetHRForException(ex2) & ((1 << 16) - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((ex2 is IOException) && (errorCode == ERROR_SHARING_VIOLATION || errorCode == ERROR_LOCK_VIOLATION))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; finally&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (stream != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.Close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}

ABOUTYOU

这是一个旧线程,但我将为其他人添加一些信息。我在编写PDF文件的程序中遇到了类似的问题,有时它们需要30秒才能呈现..这与我的watcher_FileCreated类在复制文件之前等待的时间相同。文件未锁定。在这种情况下,我检查了PDF的大小,然后等待2秒钟再比较新的大小,如果它们不相等,则该线程将休眠30秒钟,然后重试。
打开App,查看更多内容
随时随地看视频慕课网APP