|
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.
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/