【发布时间】:2015-08-27 16:25:01
【问题描述】:
在我的工具中,我内置了一项功能,可以将文件从用户指定的位置复制到另一个位置。如果我从基于网络驱动器的位置复制并复制到该位置,则复制一个 2 kb 的文件大约需要 10 分钟。这不是网络问题,因为不使用该工具也可以正常工作。
期望:将文件从一个网络位置复制到另一个位置的工具。
会发生什么:一个 1 kb 的文件需要 10 多分钟才能复制。如果我从本地驱动器到网络位置执行此操作,或者反之亦然,那就更好了。
这是我的复制代码:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (!string.IsNullOrWhiteSpace(fedPREVIEW.Text))
if (!string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text))
if (!string.IsNullOrWhiteSpace(foldercreationPATH.Text))
{
for (int i = 1; i <= 100; i++)
{
string sourcePath = @pathofsrcfilesTOCOPY.Text;
string targetPath = foldercreationPATH.Text + "\\01_SR";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
//Copy the folders from sourcepath and place into mentioned target path,
//Overwrite the folder if same file is exist in target path
foreach (string srcPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(srcPath.Replace(sourcePath, targetPath));
}
//Copy the file from sourcepath and place into mentioned target path,
//Overwrite the file if same file is exist in target path + it also copies subfolders and files from folders.
foreach (var srcPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
System.IO.File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
}
// Report progress.
backgroundWorker1.ReportProgress(i);
}
}
}
【问题讨论】:
-
请修正你的缩进。这几乎不可读
-
你可以用
if(A && B && C)代替if(A) if(B) if(C)。这是更被接受的风格。 -
您所做的不仅仅是在此处复制文件。我建议分析或检测您的代码,以确定哪些行占用了大部分时间。
-
你为什么要做这个工作100次? for (int i = 1; i
标签: c# file-transfer file-copying