【问题标题】:c# run commands through cmdc# 通过cmd运行命令
【发布时间】:2012-12-24 03:16:52
【问题描述】:

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

【问题讨论】:

  • 您是想通过 httpd.exe 获得命令提示符,还是只是想执行 httpd.exe?
  • 我只是想通过 cmd.exe 执行 httpd.exe 来阻止 apache 成为 windows 服务。
  • 是啊,你为什么不直接开始httpd.exe
  • 因为将 apache 作为服务运行
  • 你不能同时使用 shell 和重定向。

标签: c# .net apache cmd


【解决方案1】:

这对你有用吗?

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

【讨论】:

  • 通知 OPs \c 命令行参数,这不是必需的吗?
  • @GabrielGraves,谢谢cmd.exe \c httpd.exe 是一个有效的命令。
  • 这不起作用,因为它失败了,消息 httpd 不存在或不是 bla bla 命令。
  • 是的,该路径已经过测试。
【解决方案2】:

试试这个

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2010-10-16
      • 2021-05-18
      • 2016-06-03
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多