问题
I had made this code for user to select a folder so that my two files will be copied to that. The code is this:
string sourcePath = @"C:\Documents and Settings\akib\";
string fileName1 = @"untitled.jpg";
string fileName2 = @"Copyuntitled.jpg";
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
var destinationFolderName = folderBrowserDialog1.SelectedPath;
if (Directory.Exists(destinationFolderName))
{
File.Copy(sourcePath + "/" + fileName1, destinationFolderName
+ "/" + fileName1);
File.Copy(sourcePath + "/" + fileName2, destinationFolderName
+ "/" + fileName2);
}
}
But now I want to to reverse of it. That is if user have two files in some location I want to copy that to the c:\programfiles\myfolder
. So FolderBrowseDialog
can be used in such case? If yes how?
回答1:
For that you would want to use the OpenFileDialog class with the Multiselect property set to true:
string destination = @"c:\programfiles\myfolder";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (DialogResult.OK == ofd.ShowDialog()) {
foreach (string file in ofd.FileNames) {
File.Copy(file, Path.Combine(destination, Path.GetFileName(file)));
}
}
回答2:
FolderBrowseDialog can be used in exactly the same manner for getting a folder for reading or writing files.
来源:https://stackoverflow.com/questions/14632641/selecting-files-and-saving-it-to-the-defined-location-using-folder-browse-dialog