Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C#

Extend ServiceController class to change the StartupType of Windows Services

Rate me:
Please Sign up or sign in to vote.
4.53/5 (22 votes)
7 Jul 20045 min read 122.8K   2.8K   41  
How to control the windows service's StartupType and extend the functionality of the ServiceController class
using System;
using System.ServiceProcess;
using System.Management;

namespace ServiceControllerExtended
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class ServiceControllerEx:ServiceController
	{
		public ServiceControllerEx():base()
		{		}
		public ServiceControllerEx(string name):base(name)
		{		}
		public ServiceControllerEx(string name,string machineName):base(name,machineName)
		{		}

		public string Description
		{
			get
			{
				//construct the management path
				string path="Win32_Service.Name='"+this.ServiceName+"'";
				ManagementPath p=new ManagementPath(path);
				//construct the management object
				ManagementObject ManagementObj=new ManagementObject(p);
				if(ManagementObj["Description"]!=null)
				{
					return ManagementObj["Description"].ToString();
				}
				else
				{
					return null;
				}
			}
		}
		
		/// <summary>
		/// This property returns the Service startup type,
		/// it can be one of these values
		/// Automatic , Manual and Disabled
		/// and accept the same values
		/// </summary>
	
		public string  StartupType
		{
			get
			{
				if(this.ServiceName != null)
				{
					//construct the management path
					string path="Win32_Service.Name='"+this.ServiceName+"'";
					ManagementPath p=new ManagementPath(path);
					//construct the management object
					ManagementObject ManagementObj=new ManagementObject(p);
					return ManagementObj["StartMode"].ToString();
				}
				else
				{
					return null;
				}
			}
			set
			{
				if(value!="Automatic" && value!="Manual" && value!="Disabled")
					throw new Exception("The valid values are Automatic, Manual or Disabled");

				if(this.ServiceName!=null)
				{
					//construct the management path
					string path="Win32_Service.Name='"+this.ServiceName+"'";
					ManagementPath p=new ManagementPath(path);
					//construct the management object
					ManagementObject ManagementObj=new ManagementObject(p);
					//we will use the invokeMethod method of the ManagementObject class
					object[] parameters=new object[1];
					parameters[0]=value;
					ManagementObj.InvokeMethod("ChangeStartMode",parameters);				
				}
			}
		}
		
}
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Ireland Ireland
Expert in Microsoft technologies.
MCP+Site Building, MCSD, MCAD, and MCSD.NET (Early achiever)
Worked as Web developer, Trainer and software developer
Currently working as Developer support engineer.
You can contact me also through my blog http://blogs.msdn.com/mohamed_sharafs_blog/

Comments and Discussions