Brian McCarthy
Brian McCarthy

Reputation: 4784

C# WinForms App - Debugging Errors using OpenFileDialog, MultiSelect, Logging

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

Upvotes: 0

Views: 1111

Answers (3)

Hans Passant
Hans Passant

Reputation: 942000

Your code snippet doesn't match your question very well, there's no sign of you displaying the FolderBrowserDialog. There is an obvious mistake in the File.Copy() call, you pass sourceFileOpenFileDialog.FileName instead of file.

Check this answer for a way to display path names in a limited amount of space:

 using System;
 using System.ComponentModel;
 using System.Windows.Forms;

 class PathLabel : Label 
 {
   [Browsable(false)]
   public override bool AutoSize 
   {
       get { return base.AutoSize; }
       set { base.AutoSize = false; }
   }
   protected override void OnPaint(PaintEventArgs e) 
   {
      TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.PathEllipsis;
      TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, flags);
   }
}

Upvotes: 1

Brian McCarthy
Brian McCarthy

Reputation: 4784

Fixed Logging Window Message: "Source Files: System.String[]" by adding:

                 foreach (string FileName in sourceFilesList.FileNames)
                 {
                    sourceFilesList.FileNames[i] = FileName;
                    listBoxSourceFiles.Items.Add(FileName);
                    Log("Source Files: " + sourceFilesList.FileNames[i]);
                    i++;
                 }

                 // under  if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)  

Fixed 2 FileBrowserDialog boxes coming up when Selecting files by:

     if ((myStream = sourceFilesList.OpenFile()) != null)
     // deleted duplicate line 

Upvotes: 0

26071986
26071986

Reputation: 2330

To get only file name from file path use Path.GetFileName(...).

To check if multiple files were selected, you can just check openFileDialog.FileNames Length property - it's an array.

Upvotes: 1

Related Questions