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

标题: [教学] C# 实现Windows Service 服务安装完成后自动启动 [打印本页]

作者: 资讯王    时间: 2014-2-24 18:31
标题: [教学] C# 实现Windows Service 服务安装完成后自动启动
实现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. }
复制代码





欢迎光临 大马资讯论坛 - 马来西亚中文资讯平台 (http://freeinfo.com.my/) Powered by Discuz! X3.3