2012-12-24 168 views
0

我需要在cmd上執行兩條命令。儘管我的研究,我還沒有找到一個可行的解決我的問題。首先,我需要cd到目錄,然後在該目錄中運行一個exe文件。c#通過cmd運行命令

using (Process process = new Process()) 
{ 
    process.StartInfo.UseShellExecute = false; 
    process.StartInfo.RedirectStandardOutput = true; 
    process.StartInfo.RedirectStandardError = true; 
    process.StartInfo.WorkingDirectory = @"C:\Program Files\Blacksmith\bin\apache\bin"; 
    process.StartInfo.FileName = "cmd.exe"; 
    process.StartInfo.Arguments = @" \c httpd.exe"; 

    // Redirects the standard input so that commands can be sent to the shell. 
    process.StartInfo.RedirectStandardInput = true; 

    process.OutputDataReceived += ProcessOutputDataHandler; 
    process.ErrorDataReceived += ProcessErrorDataHandler; 

    process.Start(); 
    process.BeginOutputReadLine(); 
    process.BeginErrorReadLine(); 

    process.WaitForExit(); 
} 

我試圖通過cmd.exe執行httpd.exe來阻止apache作爲windows服務運行。

+1

這有什麼問題呢? –

+0

你是否試圖用httpd.exe得到命令提示符,或者你只是試圖執行httpd.exe? –

+0

我只是試圖通過cmd.exe來執行httpd.exe來阻止apache成爲windows服務。 – jgetner

回答

0

這樣可以幫你嗎?

using (Process process = new Process()) 
{ 
    process.StartInfo.UseShellExecute = false; 
    process.StartInfo.FileName = @"C:\Program Files\Blacksmith\bin\apache\bin\httpd.exe"; 

    // Redirects the standard input so that commands can be sent to the shell. 
    process.StartInfo.RedirectStandardOutput = true; 
    process.StartInfo.RedirectStandardError = true; 

    process.OutputDataReceived += ProcessOutputDataHandler; 
    process.ErrorDataReceived += ProcessErrorDataHandler; 

    process.Start(); 
    process.BeginOutputReadLine(); 
    process.BeginErrorReadLine(); 

    process.WaitForExit(); 
} 
+0

注意OPs'\ c'命令行參數,是否有必要? –

+1

\ c僅適用於cmd.exe通過參數運行命令 –

+0

@GabrielGraves,謝謝'cmd.exe \ c httpd.exe'是一個有效的命令。 –

0

試試這個

using (Process process = new Process()) 
{ 
    process.StartInfo.UseShellExecute = false; 
    process.StartInfo.RedirectStandardOutput = true; 
    process.StartInfo.RedirectStandardError = true; 
    process.StartInfo.WorkingDirectory = @"C:\Program Files\Blacksmith\bin\apache\bin"; 
    process.StartInfo.FileName = "httpd.exe"; 

    // Redirects the standard input so that commands can be sent to the shell. 
    process.StartInfo.RedirectStandardInput = true; 

    process.OutputDataReceived += ProcessOutputDataHandler; 
    process.ErrorDataReceived += ProcessErrorDataHandler; 

    process.Start(); 
    process.BeginOutputReadLine(); 
    process.BeginErrorReadLine(); 

    process.WaitForExit(); 
} 
0

我認爲,你可以嘗試的/℃,而不是\ C