[ACCEPTED]-What's the best way to set a windows service description in .net-windows

Accepted answer
Score: 27

Create a ServiceInstaller and set the description

private System.ServiceProcess.ServiceInstaller serviceInstaller = 
  new System.ServiceProcess.ServiceInstaller();
this.serviceInstaller.Description = "Handles Service Stuff";

0

Score: 15

To clarify on how to accomplish this without 16 using code:

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

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

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

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

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

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

Score: 6

After you create your service installer 4 project in VS2010, you need to add an override 3 to the Install method in the class created 2 by VS to create the registry entry for your 1 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

Score: 3

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

Setting service Description from IDE

Score: 1

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

More Related questions