2011-03-24 114 views
0

背景:我正在開發使用C#與一個OpenFileDialog和FileBrowserDialog是應該WinForms應用程序:C#WinForms應用程序 - 調試錯誤使用的OpenFileDialog,多選,登錄

  1. 啓用的多個XLS文件的選擇。
  2. 做出選擇之後,在文本框中顯示所選的xlsx文件名
  3. 複製選定的文件上的winform應用程序

底部登錄窗口合併

  • 顯示結果一個單獨的目錄,你怎麼建議解決以下任何錯誤的調試:

    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字符串文件名)迴路和控制列表框,仍然有問題瓦特/ filebrowser裝載2x,以及與「源文件:System.String []」消息

  • 回答

    1

    您的代碼片段與您的問題不太匹配,沒有顯示FolderBrowserDialog的跡象。在File.Copy()調用中有一個明顯的錯誤,您傳遞sourceFileOpenFileDialog.FileName而不是文件

    檢查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); 
        } 
    } 
    
    +0

    感謝您的迴應。我從標題中摘下了FolderBrowserDialog,我將檢查該代碼。 – 2011-03-24 16:24:39

    1

    要從文件路徑獲取文件名,請使用Path.GetFileName(...)。

    要檢查是否選擇了多個文件,只需檢查openFileDialog.FileNames Length屬性 - 它是一個數組。

    +0

    感謝您的RESP ONSE。爲什麼你的用戶名太多了?哈哈 – 2011-03-24 16:25:21

    +0

    因爲這是一個出生日期:D – 26071986 2011-03-25 08:01:25

    0

    固定記錄窗口的消息: 「源文件: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 FileBrowserDialog盒上來選擇文件時:

     if ((myStream = sourceFilesList.OpenFile()) != null) 
        // deleted duplicate line 
    
    相關問題