您如何在services.msc中使用具有不同名称的窗口服务 [英] How u can use a window service with different names in services.msc

查看:65
本文介绍了您如何在services.msc中使用具有不同名称的窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用在services.msc中安装的具有不同名称的窗口服务.msc

Use of a window service with different names installed in services.msc

推荐答案

给出了示例代码.在安装程序文件中遵循此代码

sample code given . follow this code in installer file

private void InitializeComponent()
{
    components = new System.ComponentModel.Container();

    processInstaller = new ServiceProcessInstaller();
    serviceInstaller = new ServiceInstaller();
    // Service will run under system account
    processInstaller.Account = ServiceAccount.LocalSystem;
    this.processInstaller.Password = null;
    this.processInstaller.Username = null;


    // Service will have Start Type of Manual
    serviceInstaller.StartType = ServiceStartMode.Automatic;

    string serviceDisplayName = GetConfigurationValue("ServiceDisplayName");
    string serviceName = GetConfigurationValue("ServiceName");

    if (string.IsNullOrEmpty(serviceDisplayName))
    {
        serviceDisplayName = "XXXXXXXXXXXXXXXXServiceName1";
    }

    if (string.IsNullOrEmpty(serviceName))
    {
        serviceName = "ServiceName1";
    }

    this.serviceInstaller.DisplayName = serviceDisplayName;
    this.serviceInstaller.ServiceName = serviceName;
    this.serviceInstaller.Description = "Description Service";
    //
    // ProjectInstaller
    //
    Installers.Add(serviceInstaller);
    Installers.Add(processInstaller);

}


private string GetConfigurationValue(string key)
{
    Assembly service = Assembly.GetExecutingAssembly(); // Assembly.GetAssembly(typeof(<<>DLL Name Here >));
    Configuration config = ConfigurationManager.OpenExeConfiguration(service.Location);

    if (config.AppSettings.Settings[key] != null)
    {
        return config.AppSettings.Settings[key].Value;
    } ;) 
    else
    {
        throw new IndexOutOfRangeException
            ("Settings collection does not contain the requested key: " + key);
    }
}


这篇关于您如何在services.msc中使用具有不同名称的窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆