我编写了一个应用程序,它采用一个主文件夹并复制主文件夹的所有子文件夹中的所有文件。它可以工作,但是当我复制大量文件时,表单将在一段时间内“无响应”,直到完成。
这是我正在使用的代码。
private void buttonCopy_Click(object sender, RoutedEventArgs e)
{
try
{
ArrayList existingFiles = new ArrayList();
string existingFileName = "";
string thePath = folderSelect.Text;
DirectoryInfo di = new DirectoryInfo(thePath);
FileInfo[] allFiles = di.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
foreach (FileInfo file in allFiles)
{
string fullName = file.FullName;
string fileName = System.IO.Path.GetFileName(fullName);
bool fileExists;
fileExists = File.Exists(folderSave.Text + System.IO.Path.DirectorySeparatorChar + fileName);
if (!fileExists)
{
file.CopyTo(folderSave.Text + System.IO.Path.DirectorySeparatorChar + fileName);
}
else
{
existingFileName += fileName + "\n";
existingFiles.Add(existingFileName);
}
}
if (existingFiles.Count != 0)
{
System.Windows.MessageBox.Show(existingFiles.Count + " Files already exists!\n\n" + existingFileName);
}
System.Windows.MessageBox.Show("Files copied successfully!");
}