Changing Service Startup Type

It seems to me that ServiceController class (the once I used to manage my services) doesn’t support this kind of functionality.

After googling, I found this http://www.codeproject.com/cs/system/extendservicecontroller.asp

Basically, we have to use WMI for this task. Below is the sample code

public static void SetServiceStartupType(string serviceName, 
   StartupType startupType)
{
   // construct the management path
   string path = string.Format("Win32_Service.Name='{0}'", serviceName);
   ManagementPath mPath = new ManagementPath(path);

   // construct the management object
   ManagementObject obj = new ManagementObject(mPath);

   object[] parameters = new object[1];
   parameters[0] = startupType.ToString();
   obj.InvokeMethod("ChangeStartMode", parameters);
}

There is a very good tool to generate WMI code that we can download from
http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s