我在这里看到了很多类似的问题,但没有一个能帮我解决问题。我想停止使用ServiceController对象的服务。但是它失败了,我得到了一个例外: System.ComponentModel.Win32Exception (0x80004005)。我不明白为什么..。我以“以管理员身份运行”运行程序。
ServiceController ctrl = ServiceController.GetServices().Where(s => s.ServiceName == "service_name").SingleOrDefault();
if (ctrl == null) return;
if (ctrl.Status.Equals(ServiceControllerStatus.Running))
{
try
{
ctrl.Stop();
}
catch(Exception ex)
{
Log(ex.ToString(), 3);
}
}如果我从代码中调用net命令,那么一切都正常。为什么?
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("net stop service_name");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();发布于 2020-02-03 06:14:53
在我们继续讨论ServiceController之后,发现了以下内容:
ServiceController能够使用服务,但不能StopServiceControllers Start & Stop时,ServiceController崩溃与其他服务(如使用CMD或Process.Start的PrintSpooler sc stop "servicename"F 222)
https://stackoverflow.com/questions/59980844
复制相似问题