我已经实现了这样的代码,我从中得到了这个想法。
public string BrowseFolder()
{
string filePath = string.Empty;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Multiselect = true;
openFileDialog1.Title = "Browse EXCEL File";
openFileDialog1.Filter = "Excel Files (*.xlsx)|*.xlsx";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog1.FileName;
return Path.GetDirectoryName(filePath);
}
return null;
}
public void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the source directory does not exist, throw an exception.
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory does not exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the file contents of the directory to copy.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
// Create the path to the new copy of the file.
string temppath = Path.Combine(destDirName, file.Name.ToString());
// Copy the file.
file.CopyTo(temppath, false);
}
// If copySubDirs is true, copy the subdirectories.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
// Create the subdirectory.
string temppath = Path.Combine(destDirName, subdir.Name);
// Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
好的,我们假设这两个方法都在一个类中。我很容易理解这些代码是如何工作的。它正在复制我浏览的文件夹中的所有文件。这不是我需要的。我想要实现的是只复制我的文件夹中的选定文件(多个)。
我试图操纵我的代码,但我仍然没有得到正确的解决方案。提前谢谢。
答案 0 :(得分:0)
foreach (string file in openFileDialog1.FileNames)
{
FileInfo fInfo = new FileInfo(file);
fInfo.MoveTo(newFilePath);
}
参考:https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filenames(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.io.fileinfo.moveto(v=vs.110).aspx
答案 1 :(得分:0)
您需要从OpenFileDialog中选择所有文件:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string[] filesSelected = openFileDialog1.FileNames;
}
上面的代码返回用户从OpenFileDialog中选择的所有文件。