0

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?

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
aquib Rocks
  • 113
  • 1
  • 11

2 Answers2

2

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)));
    }
}
001
  • 13,291
  • 5
  • 35
  • 66
  • and sir how i cave written this File.Copy(sourcePath + "/" + fileName2, destinationFolderName + "/" + fileName2); can be written instead of this File.Copy(file, Path.Combine(destination, Path.GetFileName(file))); your is quite complicated. – aquib Rocks Feb 01 '13 at 09:11
0

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

xpda
  • 15,585
  • 8
  • 51
  • 82