And*_*ard 0 c# file-transfer file-copying
在我的工具中,我内置了一个功能,可以将文件从用户指定的位置复制到另一个位置.如果我复制并复制到基于网络驱动器的位置,那么单个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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
你在这里做了100次同样的事情(也就是说,按照你说的这样做"复制文件",100次).
但是你说问题是复制一个文件,而提供的源显示完整的路径复制(创建目录然后复制所有文件...... 100次!).
只需调试你的代码,看看它在做什么......如果一个2kb文件的单个 System.IO.File.Copy操作需要10分钟,那么你的问题是正确的...当你发布它时,问题是错误的,或者你的测量是,因为你的代码所做的远不止是复制文件(...而且它正在做100次!)
...... 100次!