c#通过cmd运行命令

本文关键字:命令 运行 cmd 通过 | 更新日期: 2023-09-27 17:49:27

我需要在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服务运行。

c#通过cmd运行命令

这对你有用吗?

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