selecting files and saving it to the defined location using folder browse dialog

别来无恙 提交于 2019-12-11 14:22:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!