2011-03-31 115 views
0

在調試模式下,在運行C#WinForms應用程序時,我通過OpenFileDialog成功選擇了多個文件,然後將其顯示在日誌記錄窗口中,這些文件被複制到臨時目錄,我相信我會得到錯誤當試圖將excel文件轉換爲csv時。我得到以下運行時調試錯誤:C#WinForms應用程序 - 調試錯誤 - 長度不能小於零。參數名稱:長度

Error: You may not have permission to read the file or it may be corrupt. 
Reported Error: Length Can not be less than zero. 
Parameter Name: Length. 

如何解決此錯誤?

這是我在MainForm.cs

  // Consolidate Button Click Commands that executes if there are no user input errors 
    void ExecuteConsolidate() 
    { 
     string consolidatedFolder = targetFolderBrowserDialog.SelectedPath; 
     string tempfolder = targetFolderBrowserDialog.SelectedPath + "\\tempDirectory"; 
     string sFile = ""; 

     //create a temporary directory to store selected excel and csv files 
     if (!Directory.Exists(tempfolder)) 
     { 
      Directory.CreateDirectory(tempfolder); 
     }  
     try 
     { 
      for (int i = 0; i < listBoxSourceFiles.Items.Count; i++) 
      { 
       sFile = listBoxSourceFiles.Items[i].ToString(); 
       // Copy each selected xlsx files into the specified Temporary Folder 
       System.IO.File.Copy(textBoxSourceDir.Text + "\\" + sFile, tempfolder + @"\" + System.IO.Path.GetFileName(sFile), true); 
       Log("File " + sFile + " has been copied to " + tempfolder + @"\" + System.IO.Path.GetFileName(sFile));                   
      } // ends foreach  

      Process convertFilesProcess = new Process(); 
      // remove xlsx extension from filename so that we can add the .csv extension 
      string csvFileName = sourceFileOpenFileDialog.FileName.Substring(0, sourceFileOpenFileDialog.FileName.Length - 3); 

      // command prompt execution for converting xlsx files to csv 
      convertFilesProcess.StartInfo.WorkingDirectory = "I:\\CommissisionReconciliation\\App\\ConvertExcel\\"; 
      convertFilesProcess.StartInfo.FileName = "ConvertExcelTo.exe"; 
      convertFilesProcess.StartInfo.Arguments = "^" + targetFolderBrowserDialog.SelectedPath + "^" + csvFileName + ".csv"; 
      convertFilesProcess.StartInfo.UseShellExecute = true; 
      convertFilesProcess.StartInfo.CreateNoWindow = true; 
      convertFilesProcess.StartInfo.RedirectStandardOutput = true; 
      convertFilesProcess.StartInfo.RedirectStandardError = true; 
      convertFilesProcess.Start(); 

      //Process that creates all the xlsx files in temp folder to csv files. 
      Process consolidateFilesProcess = new Process(); 

      // command prompt execution for CSV File Consolidation 
      consolidateFilesProcess.StartInfo.WorkingDirectory = targetFolderBrowserDialog.SelectedPath; 
      consolidateFilesProcess.StartInfo.Arguments = "Copy *.csv ^" + csvFileName + ".csv"; 
      consolidateFilesProcess.StartInfo.UseShellExecute = false; 
      consolidateFilesProcess.StartInfo.CreateNoWindow = true; 
      consolidateFilesProcess.StartInfo.RedirectStandardOutput = true; 
      consolidateFilesProcess.StartInfo.RedirectStandardError = true; 
      consolidateFilesProcess.Start(); 

      Log("All Files at " + tempfolder + " has been converted to a csv file"); 
      Thread.Sleep(2000); 
      StreamReader sOut = consolidateFilesProcess.StandardOutput; 
      sOut.Close(); 

     } 
     catch (SecurityException ex) 
     { 
      // The user lacks appropriate permissions to read files, discover paths, etc. 
      MessageBox.Show("Security error. The user lacks appropriate permissions to read files, discover paths, etc. Please contact your administrator for details.\n\n" + 
      "Error message: " + ex.Message + "\n\n"); 
     } 
     catch (Exception ex) 
     { 
      // Could not load the image - probably related to Windows file system permissions. 
      MessageBox.Show("You may not have permission to read the file, or " + 
      "it may be corrupt.\n\nReported error: " + ex.Message); 
     } 
     try 
     {     
      if (Directory.Exists(tempfolder)) 
      { 
       Directory.Delete(tempfolder, true); 
      } 
     } 
     catch (SecurityException ex) 
     { 
      // The user lacks appropriate permissions to read files, discover paths, etc. 
      MessageBox.Show("Security error. The user lacks appropriate permissions to read files, discover paths, etc. Please contact your administrator for details.\n\n" + 
      "Error message: " + ex.Message + "\n\n"); 
     } 
     catch (Exception ex) 
     { 
      // Could not load the image - probably related to Windows file system permissions. 
      MessageBox.Show("You may not have permission to read the file, or " + 
      "it may be corrupt.\n\nReported error: " + ex.Message); 
     } 
     finally 
     { 
      // reset events 
      m_EventStopThread.Reset(); 
      m_EventThreadStopped.Reset(); 

      // create worker thread instance; 
      m_WorkerThread = new Thread(new ThreadStart(this.WorkerThreadFunction)); 
      m_WorkerThread.Start(); 
     } 
    } // ends void ExecuteConsolidate() 

感謝您尋找代碼! :) 所有有用的答案將收到票! :) 如果您需要更多信息,如workerThread方法或app.config代碼,請告訴我!

+1

禁用EXCE捕獲或打開「中斷所有錯誤」,並計算出實際拋出異常的哪一行。 – 2011-03-31 14:25:26

+0

@DanPuzey,謝謝你的迴應。如何打開所有錯誤? – 2011-03-31 18:17:58

回答

2

我想它死在這裏:

string csvFileName = sourceFileOpenFileDialog.FileName.Substring(0, 
      sourceFileOpenFileDialog.FileName.Length - 3); 

寫這樣,看看是否有幫助:

string selectedFile = sourceFileOpenFileDialog.FileName; 
string csvFileName = Path.Combine(Path.GetDirectoryName(selectedFile), 
      Path.GetFileNameWithoutExtension(selectedFile)); 

這是你行的翻譯。

但是我想,你真的想只是文件名不帶路徑:

string csvFileName = 
     Path.GetFileNameWithoutExtension(sourceFileOpenFileDialog.FileName); 
+0

感謝您的迴應! – 2011-03-31 18:17:17

3

這是最有可能的此行,是造成你的問題:

string csvFileName = sourceFileOpenFileDialog.FileName.Substring(0, sourceFileOpenFileDialog.FileName.Length - 3); 

爲什麼不使用路徑。 GetFileNameWithoutExtension獲取沒有擴展名的文件名?

+0

感謝您的回覆! – 2011-03-31 18:16:52

1

而且對所有錯誤打破:

  • 轉到 「調試」 - >對公共語言運行時異常異常(或CTRL + ALT + E)

  • 勾選 「時拋出」

  • 一旦完成與你解決,不要忘記將其復位(復位所有按鈕)

相關問題