-6

I need write a program to remove a window service. However , I only have the server name. How should I delete it ?

Superman
  • 1
  • 2
  • 1
    Well, you should learn some programming language (C# for example), then try to solve your problem by yourself and if you will have any actual problem with this - you are welcome on SO. StackOverflow is not a code-writing service. – vasily.sib Jul 13 '18 at 06:22
  • Possible duplicate of [Programmatically remove a service using C#](https://stackoverflow.com/questions/12201365/programmatically-remove-a-service-using-c-sharp) – Drag and Drop Jul 13 '18 at 06:24
  • I google your title and found 2 major match, as you must have found them already how does they not work? https://stackoverflow.com/questions/358700/how-to-install-a-windows-service-programmatically-in-c – Drag and Drop Jul 13 '18 at 06:25

1 Answers1

0

You could use the System.ServiceProcess.ServiceController class to find the service you want to remove. The bellow code will give you an array of the services installed on a machine (the ServiceName property is what you will use to indentify the service):

ServiceController.GetServices(string machineName);

To remove the service you could invoke the sc command like this:

ProcessStartInfo psi = new ProcessStartInfo("sc");
        psi.Arguments = string.Format("{0} delete \"{1}\"", machineName, serviceName).Trim();
        psi.RedirectStandardOutput = true;
        psi.UseShellExecute = false;
        var process = Process.Start(psi);
        process.WaitForExit(timeoutMilliseconds);
        var output = process.StandardOutput.ReadToEnd();
        if (process.ExitCode != 0)
        {
            throw new Exception(string.Format("Service delete for Windows Service {0} failed.", serviceName));
        }

The above code will try to delete the service and throw an exception if it can't do it after timeoutMilliseconds.