c#通过cmd运行命令

c#通过cmd运行命令,c#,.net,apache,cmd,C#,.net,Apache,Cmd,我需要在cmd上执行两个命令。尽管我做了研究,但还没有找到解决问题的可行办法。首先,我需要将cd刻录到该目录,然后在该目录中运行exe using (Process process = new Process()) { process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardEr

我需要在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

你是想用httpd.exe获取命令提示符,还是只是想执行httpd.exe?我只是想通过cmd.exe执行httpd.exe来阻止apache成为windows服务。是的,为什么不直接启动
httpd.exe
?因为apache是一种服务,所以不能同时使用shell和重定向。注意OPs
\c
命令行参数,这不是必需的吗?@GabrielGraves,谢谢
cmd.exe\c httpd.exe
是一个有效的命令。由于消息httpd不存在或不是bla-bla命令,因此该命令无效。是的,该路径已被测试。