C#創(chuàng)建Windows服務(wù)的實(shí)現(xiàn)方法
Microsoft Windows 服務(wù)能夠創(chuàng)建在它們自己的 Windows 會(huì)話中可長(zhǎng)時(shí)間運(yùn)行的可執(zhí)行應(yīng)用程序。這些服務(wù)可以在計(jì)算機(jī)啟動(dòng)時(shí)自動(dòng)啟動(dòng),可以暫停和重新啟動(dòng)而且不顯示任何用戶(hù)界面。這使服務(wù)非常適合在服務(wù)器上使用,或任何時(shí)候,為了不影響在同一臺(tái)計(jì)算機(jī)上工作的其他用戶(hù),需要長(zhǎng)時(shí)間運(yùn)行功能時(shí)使用。還可以在不同于登錄用戶(hù)的特定用戶(hù)帳戶(hù)或默認(rèn)計(jì)算機(jī)帳戶(hù)的安全上下文中運(yùn)行服務(wù)。
一、創(chuàng)建Windows 服務(wù)
1.新建一個(gè)Windows 服務(wù),并將項(xiàng)目名稱(chēng)改為“WindowsServiceDemo”,如下圖所示:
2.在解決方案資源管理器內(nèi)將Service1.cs改為MyService.cs后并點(diǎn)擊“查看代碼”圖標(biāo)按鈕進(jìn)入代碼編輯器界面,如下圖所示:
3.在代碼編輯器內(nèi)如入以下代碼,如下所示:
using System; using System.ServiceProcess; using System.IO; namespace WindowsServiceDemo { public partial class MyService : ServiceBase { public MyService() { InitializeComponent(); } string filePath = @"D:\MyServiceLog.txt"; protected override void OnStart(string[] args) { using (FileStream stream = new FileStream(filePath, FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now},服務(wù)啟動(dòng)!"); } } protected override void OnStop() { using (FileStream stream = new FileStream(filePath, FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now},服務(wù)停止!"); } } } }
4.雙擊項(xiàng)目“WindowsServiceDemo”進(jìn)入“MyService”設(shè)計(jì)界面,在空白位置右擊鼠標(biāo)彈出上下文菜單,選中“添加安裝程序”,如下圖所示:
5.此時(shí)軟件會(huì)生成兩個(gè)組件,分別為“serviceInstaller1”及“serviceProcessInstaller1”,如下圖所示:
6.點(diǎn)擊“serviceInstaller1”,在“屬性”窗體將ServiceName改為MyService,Description改為我的服務(wù),StartType保持為Manual,如下圖所示:
7.點(diǎn)擊“serviceProcessInstaller1”,在“屬性”窗體將Account改為L(zhǎng)ocalSystem(服務(wù)屬性系統(tǒng)級(jí)別),如下圖所示:
8.鼠標(biāo)右鍵點(diǎn)擊項(xiàng)目“WindowsServiceDemo”,在彈出的上下文菜單中選擇“生成”按鈕,如下圖所示:
9.至此,Windows服務(wù)已經(jīng)創(chuàng)建完畢。
二、創(chuàng)建安裝、啟動(dòng)、停止、卸載服務(wù)的Windows窗體
1.在同一個(gè)解決方案里新建一個(gè)Windows Form項(xiàng)目,并命名為WindowsServiceClient,如下圖所示:
2.將該項(xiàng)目設(shè)置為啟動(dòng)項(xiàng)目,并在窗體內(nèi)添加四個(gè)按鈕,分別為安裝服務(wù)、啟動(dòng)服務(wù)、停止服務(wù)及卸載服務(wù),如下圖所示:
3.按下F7進(jìn)入代碼編輯界面,添加引用“System.ServiceProcess”及“System.Configuration.Install”,并輸入如下代碼:
using System; using System.Collections; using System.Windows.Forms; using System.ServiceProcess; using System.Configuration.Install; namespace WindowsServiceClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string serviceFilePath = $"{Application.StartupPath}\\WindowsServiceDemo.exe"; string serviceName = "MyService"; //事件:安裝服務(wù) private void button1_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); } //事件:?jiǎn)?dòng)服務(wù) private void button2_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName); } //事件:停止服務(wù) private void button3_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName); } //事件:卸載服務(wù) private void button4_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); } } //判斷服務(wù)是否存在 private bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { return true; } } return false; } //安裝服務(wù) private void InstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; IDictionary savedState = new Hashtable(); installer.Install(savedState); installer.Commit(savedState); } } //卸載服務(wù) private void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } //啟動(dòng)服務(wù) private void ServiceStart(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Stopped) { control.Start(); } } } //停止服務(wù) private void ServiceStop(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } } } }
4.為了后續(xù)調(diào)試服務(wù)及安裝卸載服務(wù)的需要,將已生成的WindowsServiceDemo.exe引用到本W(wǎng)indows窗體,如下圖所示:
5.由于需要安裝服務(wù),故需要使用UAC中Administrator的權(quán)限,鼠標(biāo)右擊項(xiàng)目“WindowsServiceClient”,在彈出的上下文菜單中選擇“添加”->“新建項(xiàng)”,在彈出的選擇窗體中選擇“應(yīng)用程序清單文件”并單擊確定,如下圖所示:
6.打開(kāi)該文件,并將<requestedExecutionLevel level="asInvoker" uiAccess="false" />改為<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,如下圖所示:
7.IDE啟動(dòng)后,將會(huì)彈出如下所示的窗體(有的系統(tǒng)因UAC配置有可能不顯示),需要用管理員權(quán)限打開(kāi):
8.重新打開(kāi)后,在IDE運(yùn)行WindowsServiceClient項(xiàng)目;
9.使用WIN+R的方式打開(kāi)運(yùn)行窗體,并在窗體內(nèi)輸入services.msc后打開(kāi)服務(wù),如下圖所示:
10.運(yùn)行程序"Form1"點(diǎn)擊窗體內(nèi)的“安裝服務(wù)”按鈕,將會(huì)在服務(wù)中出現(xiàn)MyService,如下圖所示:
11.點(diǎn)擊“運(yùn)行服務(wù)”按鈕,將啟動(dòng)并運(yùn)行服務(wù),如下所示:
12.點(diǎn)擊“停止服務(wù)”按鈕,將會(huì)停止運(yùn)行服務(wù),如下圖所示:
13.點(diǎn)擊“卸載服務(wù)”按鈕,將會(huì)從服務(wù)中刪除MyService服務(wù)。
14.以上啟動(dòng)及停止服務(wù)將會(huì)寫(xiě)入D:\MyServiceLog.txt,內(nèi)容如下所示:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 圖解如何使用C#創(chuàng)建Windows服務(wù)
- C#創(chuàng)建Windows服務(wù)的圖文教程
- C#創(chuàng)建Windows服務(wù)與服務(wù)的安裝、卸載
- C#創(chuàng)建控制Windows服務(wù)
- C#用Topshelf創(chuàng)建Windows服務(wù)的步驟分享
- 使用C#創(chuàng)建Windows服務(wù)的實(shí)例代碼
- C#通過(guò)創(chuàng)建Windows服務(wù)啟動(dòng)程序的方法詳解
- c#創(chuàng)建windows服務(wù)入門(mén)教程實(shí)例
- c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟
- Visual Studio C#創(chuàng)建windows服務(wù)程序
相關(guān)文章
WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱(chēng)查找控件
這篇文章主要介紹了WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱(chēng)查找控件,需要的朋友可以參考下2014-08-08C# Winform實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel文件
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12winform用datagridview制作課程表實(shí)例
這篇文章主要介紹了winform用datagridview制作課程表的方法,實(shí)例分析了WinForm實(shí)現(xiàn)課程表的結(jié)構(gòu)、數(shù)據(jù)庫(kù)及調(diào)用技巧,需要的朋友可以參考下2015-01-01C#使用表達(dá)式樹(shù)(LambdaExpression)動(dòng)態(tài)更新類(lèi)的屬性值(示例代碼)
這篇文章主要介紹了C#使用表達(dá)式樹(shù)(LambdaExpression)動(dòng)態(tài)更新類(lèi)的屬性值,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01WPF利用RPC調(diào)用其他進(jìn)程的方法詳解
這篇文章主要給大家介紹了關(guān)于WPF利用RPC調(diào)用其他進(jìn)程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05