C# 系統服務添加安裝

來源:互聯網
上載者:User

Installer1.cs

 

Code
 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

 

Code
 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 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.