C# .NET - multiple selection

Asked By kiran Kumar on 26-Aug-11 02:45 AM
hi,

  I have developed one windows forms application project , in the form i have placed one button and open file dialogue , folder browser control,

 in my computer, in C:\ drive in have some compressed(zip) files ,

 my task is to copy the files by selecting with open file dialog control and move them to another drive (target folder)


 for that i write the code in button_ click as follows:

 OpenFileDialog f = new OpenFileDialog();
            f.Title = "select file to copy";
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string CopyTo = f.FileName;
                FolderBrowserDialog b1 = new FolderBrowserDialog();
                if (b1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string PasteTo = b1.SelectedPath + "//" + Path.GetFileName(f.FileName);
                    
                    MessageBox.Show(b1.SelectedPath);

                    string path = PasteTo;
                    if (File.Exists(path))
                    {
                        MessageBox.Show("file already exisit");
                    }
                    else
                    {
                        File.Copy(CopyTo, PasteTo);
                        MessageBox.Show("File Copied to New Folder");
                        return;
                    }                   
                   
                    
                    
                }
                else
                {
                    // Throw error or alert
                }



but it is not allowing me to select multiple files at a time open file dialogue control, only one compressed file is selecting,

What is the code that i have to write to select multiple files at a time and to copy them to target folder

Please give me solution , it is urgent to me to do it



aneesa replied to kiran Kumar on 26-Aug-11 02:47 AM

This link provides a nice example.

http://msdn.microsoft.com/en-us/library/cc148994.aspx

Here is a snippet

// To copy all the files in one directory to another directory. 
// Get the files in the source folder. (To recursively iterate through 
// all subfolders under the current directory, see 
// "How to: Iterate Through a Directory Tree.") 
// Note: Check for target path
if (System.IO.Directory.Exists(sourcePath)) 
{ 
 
string[] files = System.IO.Directory.GetFiles(sourcePath); 
 
 
// Copy the files and overwrite destination files if they already exist. 
 
foreach (string s in files) 
 
{ 
   
// Use static Path methods to extract only the file name from the path. 
    fileName
= System.IO.Path.GetFileName(s); 
    destFile
= System.IO.Path.Combine(targetPath, fileName); 
   
System.IO.File.Copy(s, destFile, true); 
 
} 
} 
dipa ahuja replied to kiran Kumar on 26-Aug-11 03:00 AM
For openfiledialog you are using the multiselection, but for the folderBrowseDialog there is not option for multiselect.
So you have to use the loop to move all the selected files
private void button2_Click(object sender, EventArgs e)
{
  OpenFileDialog f = new OpenFileDialog();
  f.Title = "select file to copy";
  f.Multiselect = true;
  if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
    FolderBrowserDialog b1 = new FolderBrowserDialog();
    if (b1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
      foreach (string file in f.FileNames)
      {
        string CopyTo = file;
        string FileName = b1.SelectedPath + "//" + Path.GetFileName(f.FileName);
        //Check if file exist
        if (File.Exists(FileName))
        {
          // if exist then rename it..
          string PasteTo = b1.SelectedPath + "//" + "(1)" + Path.GetFileName(FileName);
          File.Copy(CopyTo, PasteTo);
          MessageBox.Show("Duplicate File Copied to New Folder");
        }
        else
        {
          FileName = f.FileName;
          string PasteTo = b1.SelectedPath + "//" + Path.GetFileName(FileName);
          File.Copy(CopyTo, PasteTo);
          MessageBox.Show("File Copied to New Folder");
        }
      }
    }
  }
}
Anoop S replied to kiran Kumar on 26-Aug-11 07:04 AM
Use OpenFileDialog.Multiselect Property
Multiselect ->Gets or sets a value indicating whether the dialog box allows multiple files to be selected.

private void Form1_Load(object sender, EventArgs e)
{
    InitializeOpenFileDialog();
}
private void InitializeOpenFileDialog()
{
    // Set the file dialog to filter for graphics files.
    this.openFileDialog1.Filter =
        "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
        "All files (*.*)|*.*";
    // Allow the user to select multiple images.
    this.openFileDialog1.Multiselect = true;
    this.openFileDialog1.Title = "My Image Browser";
}
private void selectFilesButton_Click(object sender, EventArgs e)
{
    DialogResult dr = this.openFileDialog1.ShowDialog();
    if (dr == System.Windows.Forms.DialogResult.OK)
    {
        // Read the files
        foreach (String file in openFileDialog1.FileNames) 
        {
            // Create a PictureBox.
            try
            {
                PictureBox pb = new PictureBox();
                Image loadedImage = Image.FromFile(file);
                pb.Height = loadedImage.Height;
                pb.Width = loadedImage.Width;
                pb.Image = loadedImage;
                flowLayoutPanel1.Controls.Add(pb);
            }
            catch (SecurityException ex)
            {
                // The user lacks appropriate permissions to read files, discover paths, etc.
                MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
                    "Error message: " + ex.Message + "\n\n" +
                    "Details (send to Support):\n\n" + ex.StackTrace
                );
            }
            catch (Exception ex)
            {
                // Could not load the image - probably related to Windows file system permissions.
                MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
                    + ". You may not have permission to read the file, or " +
                    "it may be corrupt.\n\nReported error: " + ex.Message);
            }
        }
    }
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.multiselect.aspx