【问题标题】:C# WinForms App - Debugging Errors using OpenFileDialog, MultiSelect, LoggingC# WinForms 应用程序 - 使用 OpenFileDialog、MultiSelect、Logging 调试错误
【发布时间】:2011-03-24 14:57:33
【问题描述】:

背景:我正在使用 C# 和 OpenFileDialog 和 FileBrowserDialog 开发一个 WinForms 应用程序,它应该:

  1. 启用选择多个 xls 文件。
  2. 选择后,在文本框中显示选定的 xlsx 文件名
  3. 将选定的文件复制到单独的目录合并
  4. 在 winForm App 底部的日志窗口中显示结果

您建议如何解决以下调试错误:

  1. 从 FileBrowserDialog 中选择文件后,会出现另一个 FileBrowserDialog 框
  2. 只有 1 个选定的文件显示在文本框中。没有足够的空间来显示所有文件 b/c 文件路径太长了。是否可以只显示没有完整路径的文件名?除了在您推荐的文本框中显示选定的文件之外,还有更好的方法来确认 MultiSelect 在 WinForm 中的工作吗?
  3. 点击合并按钮不会将所选文件复制到合并目录或显示正确的日志文件。
  4. 我在日志记录窗口中得到以下信息:“源文件:System.String[]”

这是我的代码:

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

我会给任何有用的答案 +1 票!
感谢您的关注!

更新:更新了带有 foreach(sourceFilesList.FileNames 中的字符串 FileName)循环和列表框控件的代码,仍然存在文件浏览器加载 2x 和“源文件:System.String[]”消息的问题

【问题讨论】:

    标签: c# logging error-handling openfiledialog fileopendialog


    【解决方案1】:

    您的代码 sn-p 与您的问题不太匹配,您没有显示 FolderBrowserDialog 的迹象。 File.Copy() 调用中有一个明显的错误,您传递的是 sourceFileOpenFileDialog.FileName 而不是 file

    检查this answer 以获取在有限空间中显示路径名称的方法:

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

    【讨论】:

    • 感谢您的回复。我从标题中删除了 FolderBrowserDialog,我将检查该代码。
    【解决方案2】:

    要从文件路径中仅获取文件名,请使用 Path.GetFileName(...)。

    要检查是否选择了多个文件,您只需检查 openFileDialog.FileNames Length 属性 - 它是一个数组。

    【讨论】:

    • 感谢您的回复。为什么你的用户名有这么多数字?哈哈
    【解决方案3】:

    通过添加修复日志记录窗口消息:“源文件:System.String[]”:

                     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)  
    

    修复了选择文件时出现的 2 个文件浏览器对话框:

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

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      相关资源
      最近更新 更多