Click here to Skip to main content
5,787,682 members and growing! (20,418 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Intermediate

Adding a description to a .NET Windows Service

By Andy Hopper

This article describes how to add a description for your .NET Framework Windows Service to the Services administration tool.
C#, .NET, Win2K, WinXP, Windows, Visual Studio, Dev

Posted: 6 Feb 2002
Updated: 6 Feb 2002
Views: 180,305
Bookmarked: 96 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
59 votes for this Article.
Popularity: 8.16 Rating: 4.61 out of 5
1 vote, 1.9%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
11 votes, 21.2%
4
40 votes, 76.9%
5

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


Andy Hopper is a senior software engineer with DocuSys, a company that supplies digital medical solutions. He primarily designs and implements multi-tier backend architectures for Medical Informatics applications.
Classically trained as an electrical engineer, Andy rediscovered his long-lost love with programming while working on a Master’s degree project and changed careers in 1995 to become a self-taught software engineer. Andy realized his time with VB and ATL were at an end when he received the .NET Framework preview at the 2000 Professional Developer's Conference.
Andy has previously worked as a Resesarch Engineer at the Georgia Institute of Technology, a Senior Software Engineer at CyberCare Technologies, and a Lead Systems Architect for the American Cardiovascular Research Institute.

Flamebait: Andy thinks arrays should be 0-based.

Occupation: Web Developer
Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 46 (Total in Forum: 46) (Refresh)FirstPrevNext
GeneralThere is still a far easier way even in .Net1.1 ...memberMember 30736893:46 20 Oct '08  
NewsThere is a far easier waymemberXarium17:06 3 May '07  
GeneralRe: There is a far easier waymemberbkavanaugh3:15 1 Jun '07  
General.net 2.0memberluc_arne5:04 9 Oct '06  
GeneralNice Articlememberkrishna191:50 9 Jun '06  
GeneralA Cleaner Approach in C#memberc.sharpener10:25 6 Feb '06  
GeneralRe: A Cleaner Approach in C#memberDuoc BT18:10 15 Feb '06  
GeneralRegistry Editormemberye win zaw17:50 29 Sep '05  
GeneralOffical way to add a description to a servicemembernapalm2k11:26 10 Sep '05  
GeneralVery good!memberCaio Proiete10:06 28 May '05  
GeneralReusable component (VB.NET)memberScott Hutchinson16:26 12 May '05  
GeneralRe: Reusable component (VB.NET & C#)memberCaio Proiete10:13 28 May '05  
GeneralVB.Net Translationmemberjbaggaley2:15 23 Aug '04  
GeneralRe: VB.Net TranslationsussAnonymous6:56 7 Oct '05  
GeneralUser privileges to stop servicesussAnonymous6:17 28 Apr '04  
GeneralRunnig Winzip from Windows ServicesussAdeel Ahmad2:23 12 Apr '04  
Generalmultiple servicessussPer Søderlind5:55 16 Mar '04  
GeneralKey Flushing is requiredmemberpcsInfo8:21 9 Mar '04  
GeneralGénialsussAnonymous14:05 1 Dec '03  
Generaldefault Start ParametersmemberBill DeWeese15:52 6 Nov '03  
GeneralCOM .NET Marshalingmemberash8822:00 22 Oct '03  
GeneralRe: COM .NET MarshalingmemberPriyacb2:49 6 Jul '04  
GeneralProblems writing registry keysussMichael Gamauf2:52 5 Sep '03  
Generalrunning a exe file from windows servicemembergoodnews12323:55 27 Aug '03  
GeneralRe: running a exe file from windows servicememberOkeno Palmer7:57 29 Aug '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Feb 2002
Editor: Chris Maunder
Copyright 2002 by Andy Hopper
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project