现在的位置: 首页 > 综合 > 正文

C# 系统服务添加安装

2013年06月15日 ⁄ 综合 ⁄ 共 3134字 ⁄ 字号 评论关闭

Installer1.cs

 


 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Configuration.Install;
 5 using System.ServiceProcess;
 6 
 7 namespace WindowsService1
 8 {
 9     [RunInstaller(true)]
10     public partial class Installer1 : Installer
11     {
12         private ServiceInstaller serviceInstaller;
13         private ServiceProcessInstaller processInstaller;
14 
15         public Installer1()
16         {
17             InitializeComponent();
18             serviceInstaller = new ServiceInstaller();
19             processInstaller = new ServiceProcessInstaller();
20 
21             //设定安装后的运行模式
22             processInstaller.Account = ServiceAccount.LocalSystem;
23 
24             //如果需要改用其他用户来模拟运行,则需要改为ServiceAccount.User
25             //processInstaller.Username = "administrator";
26             //processInstaller.Password = "abcdefg";
27 
28             serviceInstaller.ServiceName = "SnowFoxService";
29             serviceInstaller.Description = "SnowFox Test Windows Service";
30 
31             serviceInstaller.AfterInstall += new InstallEventHandler(serviceInstaller_AfterInstall);
32             serviceInstaller.BeforeUninstall += new InstallEventHandler(serviceInstaller_BeforeUninstall);
33 
34             //将两个服务装到包里面去
35             Installers.Add(serviceInstaller);
36             Installers.Add(processInstaller);
37             //打完收工
38         }
39 
40         void serviceInstaller_BeforeUninstall(object sender, InstallEventArgs e)
41         {
42             //这里的目的是指当服务需要卸载的时候,则先将服务暂停运行
43             ServiceController con = new ServiceController("SnowFoxService");
44             try
45             {
46                 if (con.Status != ServiceControllerStatus.Stopped)
47                 {
48                     con.Stop();
49                 }
50             }
51             catch 
52             {
53             }
54             
55             
56             //throw new Exception("The method or operation is not implemented.");
57         }
58 
59         void serviceInstaller_AfterInstall(object sender, InstallEventArgs e)
60         {
61             //这里的目的是指当服务被安装后,需要将服务启动,因此首先需要得到这个服务的对象,然后再START
62             ServiceController con = new ServiceController(serviceInstaller.ServiceName);
63             con.Start();
64             //throw new Exception("The method or operation is not implemented.");
65         }
66     }
67 }

 

Services1.cs

 


 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Diagnostics;
 6 using System.ServiceProcess;
 7 using System.Text;
 8 
 9 namespace WindowsService1
10 {
11     public partial class Service1 : ServiceBase
12     {
13         public Service1()
14         {
15             InitializeComponent();
16         }
17 
18         protected override void OnStart(string[] args)
19         {
20             // TODO: 在此处添加代码以启动服务。
21             this.EventLog.WriteEntry("AAAAAAAAAAAAAAAAAAAAAAAAAAAA", EventLogEntryType.Information);
22         }
23 
24         protected override void OnStop()
25         {
26             // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
27 
28             
29         }
30     }
31 }
32 

抱歉!评论已关闭.