设置c#windows服务描述及允许服务与桌面交互的几种方法

方法一:

在ProjectInstaller.cs重写 install() ,Uninstall()方法


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","PI实时数据采集:能源--每天8点或20点取一次数据;汽车衡--每天1点取一次数据;设备状态--每分钟取一次数据。");
    //(Optional) Add some custom information your service will use...
    //允许服务与桌面交互
    service.SetValue("Type",0x00000110);
    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);
   }
  }

方法二:
此方法经测试,发现无效,勾是选上了,但程序启动后还是没有界面出现,好像需要电脑重启才生效

我们写一个服务,有时候要让服务启动某个应用程序,就要修改服务的属性,勾选允许服务与桌面交互,

可以用修改注册表实现,我们必须在安装后操作,所以请重写Installer的OnAfterInstall。

    
    
    
protected override void OnAfterInstall(System.Collections.IDictionary savedState) {
RegistryKey rk
= Registry.LocalMachine;
string key
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值