如何从 c# 中的 openFileDialog 复制选定的文件夹、子文件夹和文件
How to copy selected folders, subfolders and files from openFileDialog in c#
我有一个功能,可以将从 openFileDialog 中选择的所有文件夹、子文件夹文件从一个位置复制到另一个位置:
我已经制作了这个功能来复制所有选定的路径:
public void CopiarFicheiros(string CopyTo, List<string> FilesToCopy )
{
foreach (var item in FilesToCopy)
{
string DirectoryName = Path.GetDirectoryName(item);
string Copy = Path.Combine(CopyTo, DirectoryName);
if (Directory.Exists(Copy) && DirectoryName.ToLower() != "newclient" && DirectoryName.ToLower() != "newservice")
{
Directory.CreateDirectory(Copy);
File.Copy(item, Copy + @"\" + Path.GetFileName(item), true);
}
else File.Copy(item, CopyTo + @"\" + Path.GetFileName(item), true);
}
}
逻辑漏洞百出,我运行没时间了,似乎找不到合适的解决方案。
这是我从对话框中获取选定文件和文件夹的方式:
private List<string> GetFiles()
{
var Files = new List<string>();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string sFileName = openFileDialog.FileName;
string[] arrAllFiles = openFileDialog.FileNames;
Files = arrAllFiles.ToList();
}
return Files;
}
有没有人有更好的解决方案或关于我需要更改什么才能成功执行此操作的线索?
非常感谢任何帮助,谢谢
不要使用OpenFileDialog
选择文件夹。顾名思义,它不是为完成这项任务而设计的。您需要 FolderBrowserDialog
class 完成此任务。
// Copy from the current directory, include subdirectories.
DirectoryCopy(".", @".\temp", true);
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
我有一个功能,可以将从 openFileDialog 中选择的所有文件夹、子文件夹文件从一个位置复制到另一个位置:
我已经制作了这个功能来复制所有选定的路径:
public void CopiarFicheiros(string CopyTo, List<string> FilesToCopy )
{
foreach (var item in FilesToCopy)
{
string DirectoryName = Path.GetDirectoryName(item);
string Copy = Path.Combine(CopyTo, DirectoryName);
if (Directory.Exists(Copy) && DirectoryName.ToLower() != "newclient" && DirectoryName.ToLower() != "newservice")
{
Directory.CreateDirectory(Copy);
File.Copy(item, Copy + @"\" + Path.GetFileName(item), true);
}
else File.Copy(item, CopyTo + @"\" + Path.GetFileName(item), true);
}
}
逻辑漏洞百出,我运行没时间了,似乎找不到合适的解决方案。
这是我从对话框中获取选定文件和文件夹的方式:
private List<string> GetFiles()
{
var Files = new List<string>();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string sFileName = openFileDialog.FileName;
string[] arrAllFiles = openFileDialog.FileNames;
Files = arrAllFiles.ToList();
}
return Files;
}
有没有人有更好的解决方案或关于我需要更改什么才能成功执行此操作的线索? 非常感谢任何帮助,谢谢
不要使用OpenFileDialog
选择文件夹。顾名思义,它不是为完成这项任务而设计的。您需要 FolderBrowserDialog
class 完成此任务。
// Copy from the current directory, include subdirectories.
DirectoryCopy(".", @".\temp", true);
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}