【发布时间】:2018-04-17 14:05:35
【问题描述】:
我正在尝试编写一个从命令提示符运行命令并显示命令输出的代码。基本上,该命令将检查窗口服务的状态,如果它正在运行或停止。代码如下:
Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = @"cmd.exe";
startInfo.Arguments = "sc query \"My Service\" | findstr STATE";
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
}
如果我在命令行上运行命令:
sc query "My Service" | findstr STATE
这会返回我:
STATE : 1 STOPPED
但是如果我在上面运行我的 c# 代码,什么都不会发生,并且在一段时间后我的浏览器会抛出错误:
HTTP 错误 502.3 - 网关错误 指定的 CGI 应用程序遇到错误,服务器终止了进程。
不确定这里还缺少什么。
【问题讨论】:
-
你为什么执行
cmd.exe而不是sc? -
您是否尝试在 Web 应用程序中执行此操作?您可能遇到授权问题。通常,对于 500 状态,应用程序事件日志中会出现错误报告(包括更多详细信息)。
-
@mjwills 看到下面的回复为什么我不使用 SC
-
请记住,您会在此处获得本地化响应。 STATE 的拼写可能不同
-
@Richard 是的,它是一个 mvc 网络应用程序。
标签: c# command command-prompt