SC delete <service> does not work

本文关键字:does not work gt service lt SC delete | 更新日期: 2023-09-27 18:17:54

我正在从c#代码中运行sc delete命令。但是它总是返回

[SC] OpenService FAILED: "指定的服务不存在作为已安装的服务"

我尝试执行代码多次,但我仍然得到相同的错误。但是,如果我进入命令提示符并执行命令,则该服务将被成功删除。

是的,如果我在命令提示符下再次执行命令,它会给我同样的错误。所以我想知道,为什么它不从c#代码中删除,我错过了什么?

var procStartInfo = new ProcessStartInfo("cmd.exe", "/c sc delete 'myService'")
{
  RedirectStandardOutput = true,
  UseShellExecute = false,
  CreateNoWindow = false
};
var proc = new Process { StartInfo = procStartInfo };
proc.Start();
// Get the output into a string
var result = proc.StandardOutput.ReadToEnd();

SC delete <service> does not work

这是单引号。它们不起作用。

var procStartInfo = new ProcessStartInfo("cmd.exe", "/c sc delete myService")

来处理间隔服务名的双引号,然后需要转义它们。

var procStartInfo = new ProcessStartInfo("cmd.exe", "/c sc delete '"my Service'"")