Keshavdas M
Keshavdas M

Reputation: 702

SC delete <service> does not work

I am running the sc delete command from C# code. But it always returns,

[SC] OpenService FAILED 1060: "The specified service does not exist as an installed service"

I tried executing the code a number of times but I still get the same error. But If I go to the command prompt and execute the command, the service will be deleted successfully.

And yes, if I execute the command again on command prompt it gives me the same above error. So I want to know, why it does not delete from C# code, am I missing something?

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

Upvotes: 1

Views: 4304

Answers (1)

CathalMF
CathalMF

Reputation: 10055

Its the single quotes. They don't work.

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

to handle the double quotes for a spaced service name then you need to escape them.

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

Upvotes: 4

Related Questions