gpt4 book ai didi

c# - C#复制大量数据的方法

转载 作者:行者123 更新时间:2023-11-30 13:36:22 24 4
gpt4 key购买 nike

我正在使用以下方法将目录的内容复制到不同的目录。

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

}

我唯一的问题是源路径中有相当多的数据,并且在进行复制时程序变得无响应。

我想知道复制数据的选项是什么。我做了一些研究,有人建议使用缓冲区。

我还没有真正看到任何我特别了解的解决方案,因此任何清晰简洁的帮助/资源都会很棒。

感谢您的帮助!

最佳答案

如果您的目标是阻止应用程序进入无响应状态,建议的使用缓冲区的方法将无法解决您的问题。相反,请考虑使用单独的 Thread 来复制您的目录。

更好的是,使用 BackgroundWorker,它具有能够报告进度的额外好处。

关于c# - C#复制大量数据的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34538591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com