大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索

[教学] C# 实现Windows Service 服务安装完成后自动启动

[复制链接]
发表于 2014-2-24 18:31:22 | 显示全部楼层 |阅读模式
实现Windows Service 服务安装完成后自动启动 Automatically start a Windows Service on install
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration.Install;
  6. using System.Linq;
  7. using System.ServiceProcess;
  8. using System.Threading.Tasks;

  9. namespace Sync
  10. {
  11.     [RunInstaller(true)]
  12.     public partial class ProjectInstaller : System.Configuration.Install.Installer
  13.     {
  14.         public ProjectInstaller()
  15.         {
  16.             InitializeComponent();

  17.             this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
  18.             // Service will run under system account
  19.             this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;

  20.             // Service will have Start Type of Manual
  21.             this.serviceInstaller1.StartType = ServiceStartMode.Automatic;

  22.             this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);

  23.         }

  24.         private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
  25.         {
  26.             ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
  27.             sc.Start();
  28.         }
  29.     }
  30. }
复制代码

手机版|大马资讯论坛  

GMT+8, 2024-3-29 04:30 , Processed in 0.036528 second(s), 13 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表