繁体   English   中英

等到文件完全写入

[英]Wait Until File Is Completely Written

在一个目录中创建文件 ( 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();
    }
}

您面临的问题只有解决方法。

在开始复制过程之前检查文件 id 是否正在处理。 您可以调用以下函数,直到获得 False 值。

第一种方法,直接从这个答案复制:

private bool IsFileLocked(FileInfo file)
{
    FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (IOException)
    {
        //the file is unavailable because it is:
        //still being written to
        //or being processed by another thread
        //or does not exist (has already been processed)
        return true;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }

    //file is not locked
    return false;
}

方法二:

const int ERROR_SHARING_VIOLATION = 32;
const int ERROR_LOCK_VIOLATION = 33;
private bool IsFileLocked(string file)
{
    //check that problem is not in destination file
    if (File.Exists(file) == true)
    {
        FileStream stream = null;
        try
        {
            stream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        }
        catch (Exception ex2)
        {
            //_log.WriteLog(ex2, "Error in checking whether file is locked " + file);
            int errorCode = Marshal.GetHRForException(ex2) & ((1 << 16) - 1);
            if ((ex2 is IOException) && (errorCode == ERROR_SHARING_VIOLATION || errorCode == ERROR_LOCK_VIOLATION))
            {
                return true;
            }
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
    }
    return false;
}

FileSystemWatcher的文档中:

一旦创建文件,就会引发OnCreated事件。 如果一个文件被复制或传输到一个被监视的目录中,将立即引发OnCreated事件,然后是一个或多个OnChanged事件。

因此,如果复制失败(捕获异常),请将其添加到仍需要移动的文件列表中,并在OnChanged事件期间尝试复制。 最终,它应该工作。

类似(不完整;捕获特定异常,初始化变量等):

public static void listener_Created(object sender, FileSystemEventArgs e)
{
    Console.WriteLine
            (
                "File Created:\n"
               + "ChangeType: " + e.ChangeType
               + "\nName: " + e.Name
               + "\nFullPath: " + e.FullPath
            );
    try {
        File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\" + e.Name);
    }
    catch {
        _waitingForClose.Add(e.FullPath);
    }
    Console.Read();
}

public static void listener_Changed(object sender, FileSystemEventArgs e)
{
     if (_waitingForClose.Contains(e.FullPath))
     {
          try {
              File.Copy(...);
              _waitingForClose.Remove(e.FullPath);
          }
          catch {}
     }

}

这是一个旧线程,但我会为其他人添加一些信息。

我在编写 PDF 文件的程序中遇到了类似的问题,有时它们需要 30 秒才能呈现......这与我的 watcher_FileCreated 类在复制文件之前等待的时间相同。

文件没有被锁定。

在这种情况下,我检查了 PDF 的大小,然后在比较新大小之前等待 2 秒,如果它们不相等,线程将休眠 30 秒并重试。

您实际上很幸运 - 写入文件的程序将其锁定,因此您无法打开它。 如果它没有锁定它,你会复制一个部分文件,而不知道有问题。

当您无法访问某个文件时,您可以假设它仍在使用中(更好 - 尝试以独占模式打开它,并查看其他人当前是否正在打开它,而不是从 File.Copy 的失败中猜测)。 如果文件被锁定,您必须在其他时间复制它。 如果它没有被锁定,你可以复制它(这里有轻微的竞争条件)。

那“其他时间”是什么时候? 我不记得 FileSystemWatcher 何时为每个文件发送多个事件 - 检查一下,您只需忽略该事件并等待另一个事件就足够了。 如果没有,您可以随时设置时间并在 5 秒内重新检查文件。

你自己已经给出了答案; 您必须等待文件创建完成。 一种方法是通过检查文件是否仍在使用中。 一个例子可以在这里找到: 有没有办法检查文件是否在使用中?

请注意,您必须修改此代码才能使其适用于您的情况。 你可能想要像(伪代码)这样​​的东西:

public static void listener_Created()
{
   while CheckFileInUse()
      wait 1000 milliseconds

   CopyFile()
}

显然,您应该保护自己不受无限的影响while以防万一所有者应用程序永远不会释放锁。 此外,您还可以从FileSystemWatcher查看您可以订阅的其他事件。 可能有一个事件可以用来规避整个问题。

当文件以二进制(逐字节)写入时,创建 FileStream 和以上解决方案不起作用,因为文件已准备好并写入每个字节,因此在这种情况下,您需要其他解决方法:在创建文件时执行此操作或您想要开始处理文件

long fileSize = 0;
currentFile = new FileInfo(path);

while (fileSize < currentFile.Length)//check size is stable or increased
{
  fileSize = currentFile.Length;//get current size
  System.Threading.Thread.Sleep(500);//wait a moment for processing copy
  currentFile.Refresh();//refresh length value
}

//Now file is ready for any process!

因此,在快速浏览了其中的一些问题和其他类似问题后,我今天下午进行了一场快乐的追逐,试图解决使用文件作为同步(以及文件保存)方法的两个独立程序的问题。 有点不寻常的情况,但它对我来说确实突出了“检查文件是否被锁定,如果不是,则打开它”方法的问题。

问题是:在您检查文件和实际打开文件之间,文件可能会锁定。 很难追踪偶发的无法复制文件,因为如果您不寻找它,它会被另一个进程错误使用

基本的解决方法是尝试在 catch 块内打开文件,以便如果它被锁定,您可以再试一次。 这样在检查和打开之间没有经过的时间,操作系统会同时进行。

此处的代码使用 File.Copy,但它也适用于 File 类的任何静态方法:File.Open、File.ReadAllText、File.WriteAllText 等。

/// <param name="timeout">how long to keep trying in milliseconds</param>
static void safeCopy(string src, string dst, int timeout)
{
    while (timeout > 0)
    {
        try
        {
            File.Copy(src, dst);

            //don't forget to either return from the function or break out fo the while loop
            break;
        }
        catch (IOException)
        {
            //you could do the sleep in here, but its probably a good idea to exit the error handler as soon as possible
        }
        Thread.Sleep(100);

        //if its a very long wait this will acumulate very small errors. 
        //For most things it's probably fine, but if you need precision over a long time span, consider
        //   using some sort of timer or DateTime.Now as a better alternative
        timeout -= 100;
    }
}

关于并行的另一个小注意事项:这是一个同步方法,它将在等待和处理线程时阻塞其线程。 这是最简单的方法,但如果文件长时间保持锁定状态,您的程序可能会变得无响应。 Parellelism 是一个太大的话题,无法在这里深入探讨(并且您可以设置异步读/写的方法数量有点荒谬)但这是一种可以并行化的方法。

public class FileEx
{
    public static async void CopyWaitAsync(string src, string dst, int timeout, Action doWhenDone)
    {
        while (timeout > 0)
        {
            try
            {
                File.Copy(src, dst);
                doWhenDone();
                break;
            }
            catch (IOException) { }

            await Task.Delay(100);
            timeout -= 100;
        }
    }

    public static async Task<string> ReadAllTextWaitAsync(string filePath, int timeout)
    {
        while (timeout > 0)
        {
            try {
                return File.ReadAllText(filePath);
            }
            catch (IOException) { }

            await Task.Delay(100);
            timeout -= 100;
        }
        return "";
    }

    public static async void WriteAllTextWaitAsync(string filePath, string contents, int timeout)
    {
        while (timeout > 0)
        {
            try
            {
                File.WriteAllText(filePath, contents);
                return;
            }
            catch (IOException) { }

            await Task.Delay(100);
            timeout -= 100;
        }
    }
}

这是它的使用方法:

public static void Main()
{
    test_FileEx();
    Console.WriteLine("Me First!");
}    

public static async void test_FileEx()
{
    await Task.Delay(1);

    //you can do this, but it gives a compiler warning because it can potentially return immediately without finishing the copy
    //As a side note, if the file is not locked this will not return until the copy operation completes. Async functions run synchronously
    //until the first 'await'. See the documentation for async: https://msdn.microsoft.com/en-us/library/hh156513.aspx
    CopyWaitAsync("file1.txt", "file1.bat", 1000);

    //this is the normal way of using this kind of async function. Execution of the following lines will always occur AFTER the copy finishes
    await CopyWaitAsync("file1.txt", "file1.readme", 1000);
    Console.WriteLine("file1.txt copied to file1.readme");

    //The following line doesn't cause a compiler error, but it doesn't make any sense either.
    ReadAllTextWaitAsync("file1.readme", 1000);

    //To get the return value of the function, you have to use this function with the await keyword
    string text = await ReadAllTextWaitAsync("file1.readme", 1000);
    Console.WriteLine("file1.readme says: " + text);
}

//Output:
//Me First!
//file1.txt copied to file1.readme
//file1.readme says: Text to be duplicated!

您可以使用以下代码来检查文件是否可以以独占访问方式打开(即不能被其他应用程序打开)。 如果文件未关闭,您可以稍等片刻并再次检查,直到文件关闭并且您可以安全地复制它。

您仍应检查 File.Copy 是否失败,因为在您检查文件和复制文件之间,另一个应用程序可能会打开该文件。

public static bool IsFileClosed(string filename)
{
    try
    {
        using (var inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
        {
            return true;
        }
    }
    catch (IOException)
    {
        return false;
    }
}

我想在这里添加一个答案,因为这对我有用。 我使用了时间延迟,while 循环,我能想到的一切。

我打开了输出文件夹的 Windows 资源管理器窗口。 我关闭了它,一切都像魅力一样。

我希望这可以帮助别人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM