2014-09-04 78 views
1

我想從C#.NET進程類執行.exe文件,但我無法這樣做。我能夠成功地從命令提示符運行.exe。無法從C#進程類運行EXE

下面是低於C#代碼的成功的命令提示符下輸出

enter image description here

我使用。在下面的代碼之外我沒有做任何事情。

  string output = string.Empty; 
      myProcess.StartInfo.UseShellExecute = false; 
      myProcess.StartInfo.WorkingDirectory = @"C:\Windows\System32"; 
      myProcess.StartInfo.FileName = "pdftotext.exe"; 
      myProcess.StartInfo.Arguments = " -?"; 
      myProcess.StartInfo.CreateNoWindow = true; 
      myProcess.StartInfo.UseShellExecute = false; 
      myProcess.StartInfo.RedirectStandardError = true; 
      myProcess.StartInfo.RedirectStandardInput = true; 
      myProcess.StartInfo.RedirectStandardOutput = true; 
      myProcess.StartInfo.Verb = "runas"; 
      myProcess.Start(); 

      using (StreamReader streamReader = myProcess.StandardOutput) 
       output = streamReader.ReadToEnd(); 

      MessageBox.Show("Output = " + output); 

以下是上述代碼的輸出。

enter image description here

+1

是什麼讓你覺得exe文件是在該文件夾? (它可能在你的'PATH'中的任何地方用於命令行的工作。) – Richard 2014-09-04 06:31:28

+0

我複製粘貼他們的pdftotext.exe(即C:\ Windows \ System32 \ pdftotext.exe) – 2014-09-04 07:32:08

+0

我想知道你是否得到了輸出,但它不是你所期望的。當你調試時,在顯示的消息框的哪一行上;如果包含代碼中的那個,那麼執行成功。然後你可以使用像Process Explorer這樣的東西來看看這個過程是如何實際開始的。 – Richard 2014-09-04 08:22:57

回答

4
myProcess.StartInfo.FileName = "pdftotext.exe"; 

不要把整個文件路徑,它只是要求文件名。

編輯

見我嘗試相同的代碼在我結束,它正確地給我的輸出在我的控制檯:

Process myProcess = new Process(); 


      string output = string.Empty; 
      myProcess.StartInfo.UseShellExecute = false; 
      // myProcess.StartInfo.WorkingDirectory = @"C:\Windows\System32"; 
      myProcess.StartInfo.FileName = @"TRACERT.EXE"; 

      myProcess.StartInfo.CreateNoWindow = true; 
      myProcess.StartInfo.UseShellExecute = false; 
      myProcess.StartInfo.RedirectStandardError = true; 
      myProcess.StartInfo.RedirectStandardInput = true; 
      myProcess.StartInfo.RedirectStandardOutput = true; 
      //myProcess.StartInfo.Verb = "runas"; 
      myProcess.Start(); 

      //Process myProcess = Process.Start("TRACERT.EXE"); 

      using (StreamReader streamReader = myProcess.StandardOutput) 
      { 
       output = streamReader.ReadToEnd(); 
      } 


      Console.WriteLine(output); 
      Console.ReadLine(); 

enter image description here

+0

顯示我Nothing(例如MessageBox.Show(輸出)不顯示任何內容(空)) – 2014-09-04 07:36:14

+0

嘗試刷新您的streamReader。即streamReader.Flush(); – Savaratkar 2014-09-04 08:58:17

+0

streamReader沒有刷新方法StreamWriter有刷新方法 – 2014-09-04 09:13:32

0

下面是這個成功執行code.I不爲什麼我的.exe文件的輸出包含在myProcess.StandardError中,而不是myProcess.StandardOutput

我只是給它不同的參數「myProcess.StartInfo.Arguments =‘-layout d:\項目\ OCR \發票\ Sample.pdf’它工作得很好

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

     string output = string.Empty; 
     string error = string.Empty; 

     myProcess.StartInfo.UseShellExecute = false; 
     myProcess.StartInfo.FileName = "pdftotext.exe"; 
     myProcess.StartInfo.Arguments = "-layout D:\\Projects\\OCR\\Invoices\\Sample.pdf D:\\Projects\\OCR\\Invoices\\Sample.txt"; 
     myProcess.StartInfo.CreateNoWindow = true; 
     myProcess.StartInfo.UseShellExecute = false; 
     myProcess.StartInfo.RedirectStandardError = true; 
     myProcess.StartInfo.RedirectStandardInput = true; 
     myProcess.StartInfo.RedirectStandardOutput = true; 
     myProcess.StartInfo.Verb = "runas"; 
     myProcess.Start(); 

     using (StreamReader streamReader = myProcess.StandardOutput) 
      output = streamReader.ReadToEnd(); 

     using (StreamReader streamReader = myProcess.StandardError) 
      error = streamReader.ReadToEnd(); 

     if (myProcess.ExitCode > 0) 
      throw new Exception(ErrorCode(myProcess.ExitCode));