0

Used ServiceController to start my Windows service from a Windows Form application. But this is not working.

ServiceController service = new ServiceController(serviceName);

This statement not working.

Is there any other way to start Windows service from my application.

Service Display Name: Duress Alert Service
Service Name: Service1

How I can restart my service in C#?

Edit 1

According to MSDN from below answer:

ServiceController sc  = new ServiceController();
sc.ServiceName = "Service1";

It shows red line under ServiceName and error:

ServiceController does not contains the definition of ServiceName. No extension method 'ServiceName' no accepting a first argument of type controller could not be found (are you missing a using directive or an assembly reference?)

While I have added the reference of service process:

using System.ServiceProcess;

But it still showing the error.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • `ServiceController` works. – jspcal Jul 01 '18 at 08:55
  • 1
    What do you mean by `not working`? Show the exception. – Richard Schneider Jul 01 '18 at 09:23
  • There is something fishy going on with this user https://stackoverflow.com/questions/6667799/c-sharp-start-windows-service-programmatically/51109413#51109413 You answered a question on this yesterday then pasted your answer as a question today – TheGeneral Jul 01 '18 at 10:02
  • @TheGeneral yes I have posted that answer related to adding reference how is that answer is same to this question? –  Jul 01 '18 at 10:09

1 Answers1

2

You will need to use the Start and Stop methods. See https://msdn.microsoft.com/en-us/library/yb9w7ytd%28v=vs.110%29.aspx for an example.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • It show red line error under `sc.ServiceName` after creating `ServiceController`. That's ServiceController does not contains the definition of `ServiceName` –  Jul 01 '18 at 09:33