c# - C#WinForms应用程序-使用OpenFileDialog,MultiSelect,日志记录调试错误

标签 c# logging error-handling openfiledialog fileopendialog

背景:我正在使用带有OpenFileDialog和FileBrowserDialog的C#开发WinForms应用程序,该应用程序应该:

  • 启用多个xls文件的选择。
  • 选择后,在文本框中显示所选的xlsx文件名
  • 将所选文件复制到单独的目录中
  • 在winForm应用程序底部的日志记录窗口中显示结果

  • 您如何建议修复以下任何调试错误:
  • 从FileBrowserDialog中选择文件后,另一个FileBrowserDialog框出现
  • 所选文件中只有1个显示在文本框中。没有足够的空间来显示所有文件,因为文件路径太长了。是否可以只显示文件名而不显示完整路径?除了在您推荐的文本框中显示所选文件之外,还有更好的方法来确认MultiSelect在WinForm中工作吗?
  • 单击“合并”按钮不会将所选文件复制到合并目录中,也不会显示正确的日志文件。
  • 我在“日志记录”窗口中得到以下信息:“源文件: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和“Source Files:System.String []”消息

    最佳答案

    您的代码段与您的问题不太匹配,没有迹象表明您显示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);
       }
    }
    

    关于c# - C#WinForms应用程序-使用OpenFileDialog,MultiSelect,日志记录调试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5421110/

    相关文章:

    c# - 获取 FrameworkElement 边界矩形的坐标

    java - 当我编写相同的 UTF-8 时,为什么两个系统上的 Log4J2 输出不同?

    java - 事件发生时重新加载 Log4j.xml 文件配置

    php - 通知 : Undefined variable: _SESSION in "" on line 9

    r - 在R中反复调用lmrob时如何处理错误

    c# - .NET MAUI Shell - 导航后显示汉堡包图标

    c# - 如何在 MailKit 中将邮件标记为已读

    c# - 在运行存储过程 C# 之前检查文本框或日期选择器是否已更改

    c# - NLog - 结合保留天数和大小限制

    r - R-在自定义错误处理程序中从基本环境访问.Traceback