Adding a description to a .NET Windows Service






4.71/5 (54 votes)
Feb 7, 2002
1 min read

327079

1782
This article describes how to add a description for your .NET Framework Windows Service to the Services administration tool.
Introduction
Although the .NET Framework provides extremely robust Windows Service support through the classes available under the System.ServiceProcess namespace, for some reason the ability to specify your the description displayed in the Services control panel applet/MMC snap-in for your service was omitted. There exists an attribute class named ServiceProcessDescription, but it actually specifies what the Services MMC displays under the name column, and the Description column is left blank. This article will walk you through a low-level hack for adding a description by adding it directly to your service's registry key.
Most services keep their configuration information in the registry under the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\
//This code should be inserted into your ProjectInstaller class' code
public override void Install(IDictionary stateServer)
{
Microsoft.Win32.RegistryKey system,
//HKEY_LOCAL_MACHINE\Services\CurrentControlSet
currentControlSet,
//...\Services
services,
//...\<Service Name>
service,
//...\Parameters - this is where you can put service-specific configuration
config;
try
{
//Let the project installer do its job
base.Install(stateServer);
//Open the HKEY_LOCAL_MACHINE\SYSTEM key
system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
//Open CurrentControlSet
currentControlSet = system.OpenSubKey("CurrentControlSet");
//Go to the services key
services = currentControlSet.OpenSubKey("Services");
//Open the key for your service, and allow writing
service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);
//Add your service's description as a REG_SZ value named "Description"
service.SetValue("Description", "This is my service's description.");
//(Optional) Add some custom information your service will use...
config = service.CreateSubKey("Parameters");
}
catch(Exception e)
{
Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
}
}
public override void Uninstall(IDictionary stateServer)
{
Microsoft.Win32.RegistryKey system,
currentControlSet,
services,
service;
try
{
//Drill down to the service key and open it with write permission
system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
currentControlSet = system.OpenSubKey("CurrentControlSet");
services = currentControlSet.OpenSubKey("Services");
service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);
//Delete any keys you created during installation (or that your service created)
service.DeleteSubKeyTree("Parameters");
//...
}
catch(Exception e)
{
Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
}
finally
{
//Let the project installer do its job
base.Uninstall(stateServer);
}
}
After you add the above code to your ProjectInstaller class, you should see a description for your service alongside your service's name after your service is installed. In a future article, we'll look at how we can add the description as a custom attribute and extend the ServiceInstaller class to add the description for us automatically.