等到文件完全写入 [英] Wait Until File Is Completely Written

查看:119
本文介绍了等到文件完全写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建文件( FileSystemWatcher_Created )在一个目录我将它复制到另一个。但是,当我创建一个大的(> 10MB)文件,它无法复制的文件,因为它启动已经复制,当文件尚未完成创建...结果
这导致的无法复制文件,因为它是由另一个进程使用即可得到提升。 (结果
任何帮助吗?

 类节目
{
    静态无效的主要(字串[] args)
    {
        字符串路径= @D:\\果聚糖\\ FolderListenerTest \\ ListenedFolder
        FileSystemWatcher的听众;
        听众=新FileSystemWatcher的(路径);
        listener.Created + =新FileSystemEventHandler(listener_Created);
        listener.EnableRaisingEvents = TRUE;        而(到Console.ReadLine()=退出!);
    }    公共静态无效listener_Created(对象发件人,FileSystemEventArgs E)
    {
        Console.WriteLine
                (
                    文件创建:\\ n
                   +的changetype:+ e.ChangeType
                   +\\ n名称:+ e.Name
                   +\\ nFullPath:+ e.FullPath
                );
        File.Copy(e.FullPath,@D:\\果聚糖\\ FolderListenerTest \\ CopiedFilesFolder \\+ e.Name);
        Console.Read();
    }
}


解决方案

有是只为你所面临的问题的解决方法。

在检查进程是否文件ID开始副本的过程之前。您可以拨打下面的函数,直到你得到False值。

第1种方法:

 私人布尔IsFileLocked(FileInfo的文件)
{
    的FileStream流= NULL;    尝试
    {
        流= file.Open(FileMode.Open,FileAccess.ReadWrite,FileShare.None);
    }
    赶上(IOException异常)
    {
        //文件不可用,因为它是:
        //仍然被写入
        //或者被另一个线程正在处理
        //或不存在(已处理)
        返回true;
    }
    最后
    {
        如果(流!= NULL)
            stream.Close();
    }    //文件没有锁定
    返回false;
}

第2种方法:

  const int的ERROR_SHARING_VIOLATION = 32;
const int的ERROR_LOCK_VIOLATION = 33;
私人布尔IsFileLocked(字符串文件)
{
    //检查问题是不是在目标文件
    如果(File.Exists(文件)==真)
    {
        的FileStream流= NULL;
        尝试
        {
            流= File.Open(文件,FileMode.Open,FileAccess.ReadWrite,FileShare.None);
        }
        赶上(例外EX2)
        {
            //_log.WriteLog(ex2,+文件),在检查文件是否被锁定错误;
            INT错误code = Marshal.GetHRForException(EX2)及((1 <<;&下; 16) - 1);
            如果((EX2是IOException异常)及及(错误code == || ERROR_SHARING_VIOLATION错误code == ERROR_LOCK_VIOLATION))
            {
                返回true;
            }
        }
        最后
        {
            如果(流!= NULL)
                stream.Close();
        }
    }
    返回false;
}

When a file is created (FileSystemWatcher_Created) in one directory I copy it to another. But When I create a big (>10MB) file it fails to copy the file, because it starts copying already, when the file is not yet finished creating...
This causes Cannot copy the file, because it's used by another process to be raised. ;(
Any help?

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();
    }
}

解决方案

There is only workaround for the issue you are facing.

Check whether file id in process before starting the process of copy. You can call the following function until you get the False value.

1st Method:

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;
}

2nd Method:

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;
}

这篇关于等到文件完全写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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