Kevin Gale
Kevin Gale

Reputation: 4458

What's the best way to set a windows service description in .net

I have created a C# service using the VS2005 template. It works fine however the description of the service is blank in the Windows Services control applet.

Upvotes: 25

Views: 21809

Answers (6)

Nick
Nick

Reputation: 5908

Create a ServiceInstaller and set the description:

private System.ServiceProcess.ServiceInstaller serviceInstaller = 
    new System.ServiceProcess.ServiceInstaller();

this.serviceInstaller.Description = "Handles Service Stuff";

Upvotes: 27

amonroejj
amonroejj

Reputation: 633

Above and beyond the other listed solutions, make sure you're installing the service with installutil.exe rather than sc.exe. Both will add the service to your Services control panel, from where it can be started and stopped, etc. But installutil.exe is the only one that will set the "nice" features like Description and DisplayName.

Upvotes: 0

Fiach Reid
Fiach Reid

Reputation: 7069

You can also set the service name and description from the IDE, by right clicking on the "serviceInstaller" icon in the design view of ProjectInstaller class.

Setting service Description from IDE

Upvotes: 3

bizcad
bizcad

Reputation: 109

After you create your service installer project in VS2010, you need to add an override to the Install method in the class created by VS to create the registry entry for your service description.

using System;
 using System.Collections;
 using System.ComponentModel;
 using System.Configuration.Install;
 using System.ServiceProcess;
 using Microsoft.Win32;

 namespace SomeService
 {
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Overriden to get more control over service installation.
        /// </summary>
        /// <param name="stateServer"></param>      
        public override void Install(IDictionary stateServer)
        {
            RegistryKey system;

            //HKEY_LOCAL_MACHINE\Services\CurrentControlSet
            RegistryKey currentControlSet;

            //...\Services
            RegistryKey services;

            //...\<Service Name>
            RegistryKey service;

            // ...\Parameters - this is where you can put service-specific configuration
            // Microsoft.Win32.RegistryKey config;

            try
            {
                //Let the project installer do its job
                base.Install(stateServer);

                //Open the HKEY_LOCAL_MACHINE\SYSTEM key
                system = 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("MyService", true);
                //Add your service's description as a REG_SZ value named "Description"
                service.SetValue("Description", "A service that does so and so");
                //(Optional) Add some custom information your service will use...
                // config = service.CreateSubKey("Parameters");
            }
            catch (Exception e)
            {

                throw new Exception(e.Message + "\n" + e.StackTrace);
            }
        }
    }
 }

http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx

http://www.codeproject.com/KB/dotnet/dotnetscmdescription.aspx

Upvotes: 6

Tobias J
Tobias J

Reputation: 22883

To clarify on how to accomplish this without using code:

  • Add a service installer to your project as described here: http://msdn.microsoft.com/en-us/library/ddhy0byf%28v=vs.80%29.aspx

  • Open the installer (e.g. ProjectInstaller.cs) in Design view.

  • Single-click the service installer component (e.g. serviceInstaller1) or right-click it and choose Properties.

  • In the Properties pane, set the Description and/or DisplayName (this is also where you set StartType etc.) Description is probably all you want to change, although if you want to give a slightly more human-readable DisplayName (the first column in Services manager) you can also do so.

  • If desired, open the auto-generated designer file (e.g. ProjectInstaller.Designer.cs) to verify that the properties were set correctly.

  • Build the solution and install using installutil.exe or other means.

Upvotes: 15

bkelly
bkelly

Reputation:

Also you could, create a ServiceInstaller and in the properties window of the Service installer you will see a Description Property you can set. If you don't want to code it.

Upvotes: 1

Related Questions