在C#中复制大量数据的方法 [英] Method for copying large amounts of data in C#

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

问题描述

我使用下面的方法来目录中的内容复制到不同的目录。

I am using the following method to copy the contents of a directory to a different directory.

public void DirCopy(string SourcePath, string DestinationPath)
    {
        if (Directory.Exists(DestinationPath))
        {
            System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(DestinationPath);

            foreach (FileInfo file in downloadedMessageInfo.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
            {
                dir.Delete(true);
            }
        }



        //=================================================================================
        string[] directories = System.IO.Directory.GetDirectories(SourcePath, "*.*", SearchOption.AllDirectories);

        Parallel.ForEach(directories, dirPath =>
        {
            Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
        });

        string[] files = System.IO.Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories);

        Parallel.ForEach(files, newPath =>
        {
            File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
        });

    }



我唯一的问题是,有相当多的数据在源路径,而复制发生的程序变得无响应。

My only issue is that there is quite a bit of data in the source path and the program becomes non responsive while the copying is taking place.

我想知道我的选择是将数据复制。我做了一些研究,有人建议使用缓冲器。

I am wondering what my options are for copying data. I did some research and someone had recommended to use a buffer.

我还没有真正看到我的理解特别的解决方案,因此任何帮助/资源是简洁明了的将是巨大的。

I have not really seen any solution that I understand particularly well so any help/resources that are clear and concise would be great.

感谢您的帮助!

推荐答案

在Windows窗体执行长时间任务,消息线程将导致形成直到任务完成变得反应迟钝。你会需要使用线程来防止这一点。它可能很复杂,但你会需要一个BackgroundWorker:

Performing long tasks in Windows Forms, on the message thread will cause the form to become unresponsive until the task is done. You're going to need to use threading to prevent that. It can get complicated, but you're going to need a BackgroundWorker:

_Worker = new BackgroundWorker();
_Worker.WorkerReportsProgress = true;
_Worker.DoWork += Worker_DoWork;
_Worker.ProgressChanged += Worker_ProgressChanged;
_Worker.RunWorkerAsync();

这瓶坯任务的方法:

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    worker.ReportProgress(1);

    // do stuff

    worker.ReportProgress(100);
}

和报告进度的方法:

private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    switch (e.ProgressPercentage)
    {
        case 1:
            // make your status bar visible
            break;

        case 100:
            // hide it again
            break;
    }
}

您可以使用选取框进度条,但如果你。想报告的实际百分比后面,计算文件大小和进步在Worker_DoWork方法可能会变得复杂,是另外一个问题。

You can use a marquee progress bar, but if you want to report the actual percentage back, calculating file sizes and progress in your Worker_DoWork method can become complicated and is another issue.

https://msdn.microsoft.com/en-us/library/system。 componentmodel.backgroundworker(v = vs.110)的.aspx

这篇关于在C#中复制大量数据的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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