开发者

C# Windows Forms - Select and Copy Multiple Files w MultiSelect, OpenFileDialog, & FileBrowserDialog

I'm developing a WinForms application using C# with an OpenFileDialog and FileBrowserDialog and I'd like to:

  1. Enable selection of multiple xls files.
  2. After selection开发者_运维知识库 is made, Display selected xlsx filenames in textbox
  3. Copy the selected files to a separate directory Consolidated

How can I accomplish this?


Here is sample code:

        OpenFileDialog od = new OpenFileDialog();
        od.Filter = "XLS files|*.xls";
        od.Multiselect = true;
        if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string tempFolder = System.IO.Path.GetTempPath();

            foreach (string fileName in od.FileNames)
            {
                System.IO.File.Copy(fileName, tempFolder + @"\" + System.IO.Path.GetFileName(fileName));
            }
        }


There is MultiSelect property of OpenFileDialog which you need to set to true to allow selecting multiple files.

Here is a code example from MSDN which allows the user to select a multiple number of images and display them in PictureBox controls on a Form:

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


Combining both answers, here's the code I came up with to:

  • Enabling the user to Select Multiple XLSX Files (using MultiSelect, OpenFileDialog, this.OpenFileDialog Properties & FileBrowserDialog)
  • After selection is made, Display Selected XLSX Filenames in Textbox (by setting textBoxSourceFiles.Text value to sourceFileOpenFileDialog.FileNames)
  • Copy the Selected Files to a separate Consolidated directory (using foreach loop, System.IO.File.Copy, System.IO.Path.GetFileName, sourceFileOpenFileDialog.FileName)

    private void sourceFiles_Click(object sender, EventArgs e)
    {
        Stream myStream;
        OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();
    
        this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\";
        this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|All Files (*.*)|*.*";
        this.sourceFileOpenFileDialog.FilterIndex = 2;
        this.sourceFileOpenFileDialog.RestoreDirectory = true;
        this.sourceFileOpenFileDialog.Multiselect = true;
        this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation";
    
        if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        // populates text box with selected filenames 
                        textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileNames; 
                    }
                }       // ends if 
            }           // ends try 
    
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }              // ends if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
    }                  // ends public void sourceFiles_Click
    
    private void consolidateButton_Execute_Click(object sender, EventArgs e)
    {
    
        string consolidatedFolder = targetFolderBrowserDialog.SelectedPath; 
    
            foreach (String file in sourceFileOpenFileDialog.FileNames)
            {
                try
                {
                    // Copy each selected xlsx files into the specified TargetFolder 
    
                    System.IO.File.Copy(sourceFileOpenFileDialog.FileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
                    Log("File" + sourceFileOpenFileDialog.FileName + " has been copied to " + consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
                }  
            }          // ends foreach loop
    }                  // ends void consolidateButton_Execute_Click
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜