簡體   English   中英

c#通過cmd運行命令

[英]c# run commands through cmd

我需要在cmd上執行兩個命令。 盡管進行了研究,但仍未找到解決我問題的可行解決方案。 首先,我需要CD到目錄,然后在該目錄中運行一個exe。

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服務運行。

這對您有用嗎?

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();
}

嘗試這個

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();
}

我認為您可以嘗試使用/ c而不是\\ c

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM