How to start and stop windows services programmatically


This short code snippet shows how you can access the status of any windows services using C# or VB.NET. Also, it is easy to start and stop windows services programmatically.

Introduction



This short code snippet shows how you can start and stop windows services and also how to find the current status of any windows services.

System.ServiceProcess.ServiceController class



You can use the .NET class System.ServiceProcess.ServiceController to work with the windows services.

Include the namespace in the top of your class:

VB.NET

Imports System.ServiceProcess

C#

using System.ServiceProcess;


Create an instance of the class:
VB.NET

dim controller as new ServiceController

C#

ServiceController controller = new ServiceController();


See the following code which gets the status of the IIS Admin service. Also, you can start and stop the service using this class.

VB.NET

controller.MachineName = "."
controller.ServiceName = "IISADMIN"
dim status as string = controller.Status.ToString

' Stop the service
controller.Stop()

' Start the service
controller.Start()

C#

controller.MachineName = ".";
controller.ServiceName = "IISADMIN";
string status = controller.Status.ToString();

// Stop the service
controller.Stop();

// Start the service
controller.Start();


Comments

Author: Yuri Sho22 Apr 2008 Member Level: Bronze   Points : 0

When I try happen this error:

System.InvalidOperationException = {"Cannot stop wuauserv service on computer '.'."}

InnerException = {"The service has not been started"}

System.ComponentModel.Win32Exception = {"The service has not been started"}

ErrorCode = -2147467259

NativeErrorCode = 1062

How to correct? ThX!

Author: Vikas Kumar Singh14 May 2008 Member Level: Bronze   Points : 2

Hi,

Check for the service's startup type if disable. Keep it either Automatic or Manual.

Regards,
Vikas Singh

Author: Sunil09 Nov 2009 Member Level: Bronze   Points : 2

Here is the code with Exception handling.
private void btnStart_Click(object sender, EventArgs e)
{
ServiceController sc = new System.ServiceProcess.ServiceController(strSVCName);
try
{

if (sc.Status.Equals(ServiceControllerStatus.Paused))
{
sc.Continue();
sc.WaitForStatus(ServiceControllerStatus.Running);
btnStart.Enabled = false;
btnStop.Enabled = true;
btnPause.Enabled = true;
lblServiceStatus.Text = "Running";
}
else
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
btnStart.Enabled = false;
btnStop.Enabled = true;
lblServiceStatus.Text = "Running";
if (sc.CanPauseAndContinue)
btnPause.Enabled = true;
}
}

}
catch (Exception ex)
{
MessageBox.Show("Could not start " + strSVCName + " Service.\n Error : " + ex.Message.ToString());
}
finally
{
sc.Close();
}
}

private void btnStop_Click(object sender, EventArgs e)
{

ServiceController sc = new System.ServiceProcess.ServiceController(strSVCName);
try
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);


if (sc.Status.Equals(ServiceControllerStatus.Stopped))
{
btnStart.Enabled = true;
btnStop.Enabled = false;
btnPause.Enabled = false;
lblServiceStatus.Text = "Stopped";
}
}
catch (Exception ex)
{
MessageBox.Show("Could not stop " + strSVCName + " Service.\n Error : " + ex.Message.ToString());
}
finally
{
sc.Close();
}
}

Author: MathiasE20 Nov 2009 Member Level: Bronze   Points : 2

Hi,
Thanks for this great post!
I tried it and it worked fine on a Windows XP system.

But on Windows 7 I get an error:
"Service [ServiceName] cannot be opened on computer [MachineName]"

The service is a local service.

With "sc stop [ServiceName]" at the command-prompt I got:
[SC] OpenService Error 5: Access denied
Navigating through the options of services.msc, wmimgmt.msc, gpedit.msc I couldn't find the specific option to get this access.
How is it possible to control that and to start and stop a service with the ServiceController-Class?

Thanks for help!
MathiasE

Author: Sunil20 Nov 2009 Member Level: Bronze   Points : 1

Hi,

As always Vista onwards all the os are headache for developer.
Open command run as an administrator. and then try.
it will work.

Regards,
Sunil G.

Author: Sunil21 Nov 2009 Member Level: Bronze   Points : 1

Hi,

As always Vista onwards all the os are headache for developer.
Open command run as an administrator. and then try.
it will work.

Regards,
Sunil G.

Author: MathiasE21 Nov 2009 Member Level: Bronze   Points : 1

Thanks, Sunil!
It works. But it seems no good idea, to tell users of my app to run it as administrator, since most of them aren't. (And the IT people do not want the service running permanently.) Is there no way to control a specific service access programmatically?
Regards,
MathiasE

Author: Sunil21 Nov 2009 Member Level: Bronze   Points : 1

Hi,

Just add administrator group to your application. This should work for u.

Regards,
Sunil G.

Author: Mohammad Talha01 Dec 2010 Member Level: Bronze   Points : 1

Hi all
i have already done this code and it works fine in Visual Studio but Not work in IIS on same PC or Another PC in LAN.
My Application work in Network through IIS.
it gives error about not find MachineName.
I already give my computer name and also give my IP address in Machine Name but still give same error.
I dont know what is the problem.
Once again Its work very well in visual studio but not in IIS on same PC.
Pls give me solution ASAP
Thanks in Advance

Author: mona09 Mar 2011 Member Level: Bronze   Points : 0

It is very useful. Thank you for your article.

Guest Author: Suman23 Sep 2013

vb.net windows service not running automatically in windows 2008 R2

Guest Author: Suman23 Sep 2013

vb.net windows service not running automatically



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: