2011-03-31 121 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)

  • 勾选 “时抛出”

  • 一旦完成与你解决,不要忘记将其复位(复位所有按钮)

相关问题