c#通过cmd运行命令

时间:2012-12-24 03:16:52

标签: c# .net apache 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服务运行。

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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();
}

答案 2 :(得分:0)

我认为您可以尝试/ c而不是\ c