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

前端 未结 5 899
离开以前
离开以前 2021-02-18 16:12

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.

5条回答
  •  囚心锁ツ
    2021-02-18 16:48

    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();
            }
            /// 
            /// Overriden to get more control over service installation.
            /// 
            ///       
            public override void Install(IDictionary stateServer)
            {
                RegistryKey system;
    
                //HKEY_LOCAL_MACHINE\Services\CurrentControlSet
                RegistryKey currentControlSet;
    
                //...\Services
                RegistryKey services;
    
                //...\
                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

提交回复
热议问题