2010-03-30 85 views
2

我使用lame進行我的項目之一的代碼轉換。問題是,當我從C#調用跛腳時,會彈出一個DOS窗口。有什麼辦法可以壓制這個嗎?使用命令行參數在C#中運行exe,抑制dos窗口

這裏是我到目前爲止的代碼:

Process converter = 
    Process.Start(lameExePath, "-V2 \"" + waveFile + "\" \"" + mp3File + "\""); 

converter.WaitForExit(); 
+2

如何向我們展示一些代碼? – 2010-03-30 14:39:04

回答

8

你嘗試類似:

using(var process = new Process()) 
{ 
    process.StartInfo.FileName = "..."; 
    process.StartInfo.WorkingDirectory = "..."; 
    process.StartInfo.CreateNoWindow = true; 
    process.StartInfo.UseShellExecute = false; 
    process.Start(); 
} 
+0

process.WaitForExit();如果使用了這種方法,那麼這種方法並不適用是否有可能避免Dos窗口以及保持程序暫停,直到進程終止? – Egon 2010-03-31 13:01:11

+0

@Egon:WaitForExit()有什麼問題;?我正在使用它,迄今爲止我從未遇到任何問題。 – tanascius 2010-03-31 13:57:59

+0

我正在使用lame來更改文件編碼。在接下來的幾行我正在播放新編碼的文件。如果我在沒有dos窗口的情況下執行此操作,我會認爲該文件不存在。 – Egon 2010-03-31 14:25:00

3

假設你通過Process.Start調用它,你可以使用接受ProcessStartInfo具有過載其CreateNoWindow屬性設置爲true及其UseShellExecute設置爲false

ProcessStartInfo對象也可以通過Process.StartInfo屬性進行訪問,並且可以在啓動過程之前直接設置對象(如果您需要設置少量屬性,則可以更容易)。

3
Process bhd = new Process(); 
bhd.StartInfo.FileName = "NSOMod.exe"; 
bhd.StartInfo.Arguments = "/mod NSOmod /d"; 
bhd.StartInfo.CreateNoWindow = true; 
bhd.StartInfo.UseShellExecute = false; 

是另一種方式。

2

這是我的代碼,做了類似的事情,(以及讀取輸出和返回代碼)

 process.StartInfo.FileName = toolFilePath; 
     process.StartInfo.Arguments = parameters; 

     process.StartInfo.UseShellExecute = false; // needs to be false in order to redirect output 
     process.StartInfo.RedirectStandardOutput = true; 
     process.StartInfo.RedirectStandardError = true; 
     process.StartInfo.RedirectStandardInput = true; // redirect all 3, as it should be all 3 or none 
     process.StartInfo.WorkingDirectory = Path.GetDirectoryName(toolFilePath); 

     process.StartInfo.Domain = domain; 
     process.StartInfo.UserName = userName; 
     process.StartInfo.Password = decryptedPassword; 

     process.Start(); 

     output = process.StandardOutput.ReadToEnd(); // read the output here... 

     process.WaitForExit(); // ...then wait for exit, as after exit, it can't read the output 

     returnCode = process.ExitCode; 

     process.Close(); // once we have read the exit code, can close the process