我有一个if语句,用于检查目录是否存在,是否存在,将该文件夹复制到指定位置。
整个复制过程位于预定文件夹位置的数组上,for循环遍历数组并在每个位置复制文件夹及其数据。
目前有200个不同的地方要复制,还有更多要添加。
我正在尝试围绕复制这些200多个文件夹实现进度条,但继续运行错误,我认为我遇到的问题主要是由于数组,我见过的教程(大不相同)彼此)只涵盖了基本的文件复制。
有关如何使进度条工作的任何帮助或提示将非常感激:)
for (int i = 0; i < pathArray.Length; i++)
{
string sourcePath = pathArray[i];
//MISSING CODE
if (System.IO.Directory.Exists(sourcePath))
{
System.IO.Directory.CreateDirectory(targetPathProper);
foreach (string dirPath in System.IO.Directory.GetDirectories(sourcePath,"*",
(System.IO.SearchOption.AllDirectories)))
{
System.IO.Directory.CreateDirectory(dirPath.Replace(sourcePath,
targetPathProper));
}
foreach (string newPath in System.IO.Directory.GetFiles(sourcePath, "*",
(System.IO.SearchOption.AllDirectories)))
{
System.IO.File.Copy(newPath, newPath.Replace(sourcePath,
targetPathProper), true);
}
} //end if
} // end for
答案 0 :(得分:2)
你说你得到一个错误,你得到了什么错误?
对于进度条,您可以简单地在数组中的每个基本目录处递增。没有真正需要为每个文件递增。或者,如果需要在每个文件上指示进度,则可以有两个进度条。
progressBar1.Maximum = pathArray.Length;
progressBar1.Value = 0;
for (int i = 0; i < pathArray.Length; i++)
{
string sourcePath = pathArray[i];
progressBar1.Value++;
if (System.IO.Directory.Exists(sourcePath))
{
System.IO.Directory.CreateDirectory(targetPathProper);
string[] subDirs = System.IO.Directory.GetDirectories(sourcePath,"*",(System.IO.SearchOption.AllDirectories))
progressBar2.Maximum = subDirs.Length;
progressBar2.Value = 0;
foreach (string dirPath in subDirs)
{
progressBar2.Value++;
System.IO.Directory.CreateDirectory(dirPath.Replace(sourcePath,
targetPathProper));
Application.DoEvents();
}
progressBar2.Value = 0;
foreach (string newPath in subDirs)
{
progressBar2.Value++;
System.IO.File.Copy(newPath, newPath.Replace(sourcePath,
targetPathProper), true);
Application.DoEvents();
}
} //end if
} // end for
答案 1 :(得分:2)
string[] subDirs =
System.IO.Directory.GetDirectories(SourcePath, "*",
(System.IO.SearchOption.AllDirectories));
int subFiles = System.IO.Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories).Length;
progressBar1.Maximum = subFiles+(subDirs.Length);
progressBar1.Value = 0;
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
SearchOption.AllDirectories))
{
progressBar1.Value++;
progressBar1.PerformStep();
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
Application.DoEvents();
}
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories))
{
progressBar1.Value++;
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
Application.DoEvents();
}
答案 2 :(得分:1)
您可以使用Backgroundworker但是如果您想使用其他方法 这里有一个带有可下载源代码的链接,这也有助于你开始,因为我假设这是一个winforms应用程序
How to copy files in C# with a customizable progress indicator and or progress bar
答案 3 :(得分:0)
试
{
string [] subDirs = System.IO.Directory.GetDirectories(SourcePath,“”,(System.IO.SearchOption.AllDirectories));
int subFiles = System.IO.Directory.GetFiles(SourcePath,“。*”,SearchOption.AllDirectories).Length;
progressBar1.Maximum = subFiles +(subDirs.Length);
progressBar1.Value = 0;
foreach(Directory.GetDirectories中的字符串dirPath(SourcePath,“*”,
SearchOption.AllDirectories))
{
progressBar1.Value ++;
Directory.CreateDirectory(dirPath.Replace(SourcePath,DestinationPath));
Application.DoEvents();
}
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories))
{
progressBar1.Value++;
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
Application.DoEvents();
}
}