Question

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?

Was it helpful?

Solution

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)));
    }
}

OTHER TIPS

FolderBrowseDialog can be used in exactly the same manner for getting a folder for reading or writing files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top