Click here to Skip to main content
Click here to Skip to main content

Adding a description to a .NET Windows Service

By , 6 Feb 2002
 

Sample Image - example.gif

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\ key. This is where the Service Control Manager (SCM) looks to get a list of services installed on a machine, and the Services control panel uses the SCM to list and modify the services. If you look under the key for a service, you'll see several entries, but we're interested in one in particular: the Description value. This is a REG_SZ (string) value, and this is where the SCM looks to get a service's description. We'll now take advantage of this arcane knowledge in the code below (you may download the source by clicking here):

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

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

About the Author

Andy Hopper
Web Developer
United States United States
Member
Andy Hopper is a Enterprise Architect for Wintellect (http://www.wintellect.com), a company that supplies software training, consulting, and debugging services.
 
Classically trained as an electrical engineer, Andy rediscovered his long-lost love with programming while working on a PhD project and changed careers in 1995 to become a self-taught software engineer. Andy realized his time with C++, VB and ATL were at an end when he received the .NET Framework preview at the 2000 Professional Developer's Conference.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberVasileI27 Jul '10 - 16:07 
Generalgood article for its timememberDonsw29 Nov '09 - 9:32 
GeneralThere is still a far easier way even in .Net1.1 ...memberMember 307368920 Oct '08 - 2:46 
NewsThere is a far easier waymemberXarium3 May '07 - 16:06 
GeneralRe: There is a far easier waymemberbkavanaugh1 Jun '07 - 2:15 
General.net 2.0memberluc_arne9 Oct '06 - 4:04 
GeneralNice Articlememberkrishna199 Jun '06 - 0:50 
GeneralA Cleaner Approach in C#memberc.sharpener6 Feb '06 - 9:25 
GeneralRe: A Cleaner Approach in C#memberDuoc BT15 Feb '06 - 17:10 
GeneralRegistry Editormemberye win zaw29 Sep '05 - 16:50 
GeneralOffical way to add a description to a servicemembernapalm2k10 Sep '05 - 10:26 
GeneralVery good!memberCaio Proiete28 May '05 - 9:06 
GeneralReusable component (VB.NET)memberScott Hutchinson12 May '05 - 15:26 
GeneralRe: Reusable component (VB.NET &amp; C#)memberCaio Proiete28 May '05 - 9:13 
GeneralVB.Net Translationmemberjbaggaley23 Aug '04 - 1:15 
GeneralRe: VB.Net TranslationsussAnonymous7 Oct '05 - 5:56 
GeneralUser privileges to stop servicesussAnonymous28 Apr '04 - 5:17 
Hi,
 
I run my service in context of a normal user. The idea was to log serious errors and then to stop the service by it self. But since the user does not seam to have the correct privilege it does not stop when using code like
 
ServiceController sc = new ServiceController("MyService");
sc.Stop();
 
instead I get an exception.
 
When I run my service in context of LocalSystem stopping works.
The thing is I don't want to give my user account privileges like
'act as a part of the operating system'.
 
Would have been very nice if they also had implemented sort of privilege
'can stop windows services'
 
Thanks for any idea or help.

GeneralRunnig Winzip from Windows ServicesussAdeel Ahmad12 Apr '04 - 1:23 
Generalmultiple servicessussPer Søderlind16 Mar '04 - 4:55 
GeneralKey Flushing is requiredmemberpcsInfo9 Mar '04 - 7:21 
GeneralG&#233;nialsussAnonymous1 Dec '03 - 13:05 
Generaldefault Start ParametersmemberBill DeWeese6 Nov '03 - 14:52 
GeneralCOM .NET Marshalingmemberash8822 Oct '03 - 21:00 
GeneralRe: COM .NET MarshalingmemberPriyacb6 Jul '04 - 1:49 
GeneralProblems writing registry keysussMichael Gamauf5 Sep '03 - 1:52 
Generalrunning a exe file from windows servicemembergoodnews12327 Aug '03 - 22:55 
GeneralRe: running a exe file from windows servicememberOkeno Palmer29 Aug '03 - 6:57 
Questionwhy so many levels?memberav41230 Jul '03 - 11:33 
AnswerRe: why so many levels?memberthe_cRaCk_6 Aug '03 - 22:21 
GeneralRe: why so many levels?memberAndy Hopper7 Aug '03 - 1:24 
GeneralRe: why so many levels?memberRGabo19 Nov '04 - 17:27 
AnswerRe: why so many levels?memberakosows7 Jul '05 - 23:39 
GeneralExcellentmemberzzzzip16 Jun '03 - 19:27 
QuestionWhat is Windows Service?sussAnonymous21 Apr '03 - 20:33 
AnswerRe: What is Windows Service?memberxrenvam22 Apr '03 - 15:12 
Generalthrows exceptionmemberxrenvam14 Apr '03 - 14:41 
GeneralAdd Start Parameters to Windows ServicememberDavid Spectorman14 Apr '03 - 3:49 
GeneralRe: Add Start Parameters to Windows Servicememberbooze4 Jul '03 - 8:46 
GeneralRe: Add Start Parameters to Windows ServicememberDavid Spectorman26 Nov '03 - 1:11 
GeneralThanks!memberrado20 Feb '03 - 21:35 
Questionwhat about....memberFiguerres20 Jan '03 - 7:24 
AnswerRe: what about....memberFiguerres21 Jan '03 - 5:29 
GeneralNiiicememberkeno8 Jan '03 - 16:17 
GeneralDescriptions to .NET Servicesmembervbscripteater26 Aug '02 - 8:03 
GeneralRe: Descriptions to .NET ServicessussAnonymous26 Aug '02 - 16:06 
GeneralRe: Descriptions to .NET ServicessussAnonymous18 Dec '02 - 17:33 
GeneralRe: Descriptions to .NET ServicesmemberJim Rogers28 Nov '03 - 4:46 
GeneralVery nicememberCareBear1 Jul '02 - 16:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 7 Feb 2002
Article Copyright 2002 by Andy Hopper
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid