我想探索目录,选择一个文件夹,然后将现有文件夹的内容复制到这个新目录。
我正在使用此代码,但它不起作用我要复制它的文件夹是:C:\Project
DialogResult result = fd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show(fd.SelectedPath.ToString());
}
var _SelectedPath = fd.SelectedPath.ToString();
string sourceFile = @"C:\Project";
string destinationFile = _SelectedPath;
string fileName;
//System.IO.Directory.Move(sourceFile, @"_SelectedPath");
if (!System.IO.Directory.Exists(@"_SelectedPath"))
{
System.IO.Directory.CreateDirectory(@"_SelectedPath");
}
if (System.IO.Directory.Exists(sourceFile))
{
string[] files = System.IO.Directory.GetFiles(sourceFile);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
_SelectedPath = System.IO.Path.Combine(@"_SelectedPath", fileName);
System.IO.File.Copy(s, @"_SelectedPath", true);
}
}