2011-02-16 104 views
0

嗨,對於下面的代碼,爲什麼我在錯誤文本文件中獲取此輸出?運行批處理文件,獲取錯誤和處理輸出到一個文本文件

「'ha57061'不被識別爲內部或外部命令, 可操作程序或批處理文件。」

我的用戶名是cha57061。爲什麼我錯過了「c」和「'」?如果我的代碼錯誤,請糾正我。

System.Diagnostics.Process runantc = new System.Diagnostics.Process(); 

      runantc.StartInfo.FileName = "C:/Documents and Settings/Cha57061/Desktop/New Folder/WPF/WpfApplication1/WpfApplication1/cmd.exe"; 
      runantc.StartInfo.Arguments = "antc.bat"; 
      runantc.StartInfo.UseShellExecute = false; 
      runantc.StartInfo.RedirectStandardOutput = true; 
      runantc.StartInfo.RedirectStandardError = true; 
      runantc.Start(); 

      string procOutput = runantc.StandardOutput.ReadToEnd(); 
      string procError = runantc.StandardError.ReadToEnd(); 



      TextWriter outputlog = new StreamWriter("C:/Documents and Settings/Cha57061/Desktop/New Folder/WPF/WpfApplication1/WpfApplication1/processoutput.txt"); 
      outputlog.Write(procOutput); 
      outputlog.Close(); 


      TextWriter outputerror = new StreamWriter("C:/Documents and Settings/Cha57061/Desktop/New Folder/WPF/WpfApplication1/WpfApplication1/error.txt"); 
      outputerror.Write(procError); 
      outputerror.Close(); 

回答

1

,如果它解決您的問題,但是這是第一次我看到在C#.NET中使用正斜槓(/)的文件路徑,不知道如果他們自動轉換成(\)不知道。 你可以嘗試重寫你的路下面

"C:\\Directory\\File" //the double slash is necessary since (\) indicates an escape character is to come) 
@"C:\Directory\" // the @ modifier changes the default behavior and escape characters are not considered the same way 

指示\ c是一個轉義字符,如下所述:http://msdn.microsoft.com/en-us/library/4edbef7e%28v=vs.71%29.aspx

+0

不要緊,我的答案,我看到你正在獲取批處理文件執行正確,因爲你得到錯誤文本文件的輸出。所以SLaks是正確的,你的批處理文件有一個錯誤 – rsalmeidafl 2011-02-16 01:23:07

0

這是錯誤的方式來運行一個批處理文件。
您應該將FileName直接設置爲您的.bat文件的路徑。

要回答你的問題,你的批處理文件有問題。