Question

Background: I'm developing a WinForms application using C# with an OpenFileDialog and FileBrowserDialog that is supposed 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
  4. Show results in logging window on the bottom of the winForm App

How do you recommend to fix any of the following Errors in Debugging:

  1. After selecting the files from the FileBrowserDialog, another FileBrowserDialog box comes up
  2. Only 1 of the files selected shows up in the textbox. There's not enough room to display all files b/c the file paths are so long. Would it be possible to just display the filename without the full path? Is there a better way for confirming the MultiSelect worked in a WinForm besides displaying the selected files in a textbox that you recommend?
  3. Hitting the Consolidate button does not copy the selected files to the consolidated directory or display the correct log files.
  4. I get the following in the Logging Window: "Source Files: System.String[]"

Here's my code:

private void sourceFiles_Click(object sender, EventArgs e)
{
    Stream myStream;
    int i = 0;
    OpenFileDialog sourceFilesList = 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)
                {
                     Log("Source Files: " + sourceFilesList.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

I will give +1 up-votes for any helpful answers!
Thanks for looking!

Update: Updated code w/ a foreach (string FileName in sourceFilesList.FileNames) loop and a listbox control, still having problems w/ filebrowser loading 2x, and the "Source Files: System.String[]" message

No correct solution

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