C#創(chuàng)建Windows Service(Windows 服務(wù))的方法步驟
Windows Service這一塊并不復(fù)雜,但是注意事項(xiàng)太多了,網(wǎng)上資料也很凌亂,偶爾自己寫也會(huì)丟三落四的。所以本文也就產(chǎn)生了,本文不會(huì)寫復(fù)雜的東西,完全以基礎(chǔ)應(yīng)用的需求來(lái)寫,所以不會(huì)對(duì)Windows Service寫很深入。
本文介紹了如何用C#創(chuàng)建、安裝、啟動(dòng)、監(jiān)控、卸載簡(jiǎn)單的Windows Service 的內(nèi)容步驟和注意事項(xiàng)。
一、創(chuàng)建一個(gè)Windows Service
1)創(chuàng)建Windows Service項(xiàng)目
2)對(duì)Service重命名
將Service1重命名為你服務(wù)名稱,這里我們命名為ServiceTest。
二、創(chuàng)建服務(wù)安裝程序
1)添加安裝程序
之后我們可以看到上圖,自動(dòng)為我們創(chuàng)建了ProjectInstaller.cs以及2個(gè)安裝的組件。
2)修改安裝服務(wù)名
右鍵serviceInsraller1,選擇屬性,將ServiceName的值改為ServiceTest。
3)修改安裝權(quán)限
右鍵serviceProcessInsraller1,選擇屬性,將Account的值改為L(zhǎng)ocalSystem。(如果沒(méi)有這個(gè)步驟在安裝的服務(wù)的時(shí)候會(huì)出現(xiàn)輸入用戶密碼的狀況)
三、寫入服務(wù)代碼
1)打開(kāi)ServiceTest代碼
右鍵ServiceTest,選擇查看代碼。
2)寫入Service邏輯
添加如下代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; namespace WindowsServiceTest { public partial class ServiceTest : ServiceBase { public ServiceTest() { InitializeComponent(); } protected override void OnStart(string[] args) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start."); } } protected override void OnStop() { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop."); } } } }
這里我們的邏輯很簡(jiǎn)單,啟動(dòng)服務(wù)的時(shí)候?qū)憘€(gè)日志,關(guān)閉的時(shí)候再寫個(gè)日志。
四、創(chuàng)建安裝腳本
在項(xiàng)目中添加2個(gè)文件如下(必須是ANSI或者UTF-8無(wú)BOM格式):
1)安裝腳本Install.bat
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsServiceTest.exe Net Start ServiceTest sc config ServiceTest start= auto
2)卸載腳本Uninstall.bat
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsServiceTest.exe
3)安裝腳本說(shuō)明
第二行為啟動(dòng)服務(wù)。
第三行為設(shè)置服務(wù)為自動(dòng)運(yùn)行。
這2行視服務(wù)形式自行選擇。
4)腳本調(diào)試
如果需要查看腳本運(yùn)行狀況,在腳本最后一行加入pause
五、在C#中對(duì)服務(wù)進(jìn)行控制
0)配置目錄結(jié)構(gòu)
簡(jiǎn)歷一個(gè)新WPF項(xiàng)目,叫WindowsServiceTestUI,添加對(duì)System.ServiceProcess的引用。
在WindowsServiceTestUI的bin\Debug目錄下建立Service目錄。
將WindowsServiceTest的生成目錄設(shè)置為上面創(chuàng)建的Service目錄。
生成后目錄結(jié)構(gòu)如下圖
1)安裝
安裝時(shí)會(huì)產(chǎn)生目錄問(wèn)題,所以安裝代碼如下:
string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service"; Process process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "Install.bat"; process.StartInfo.CreateNoWindow = true; process.Start(); System.Environment.CurrentDirectory = CurrentDirectory;
2)卸載
卸載時(shí)也會(huì)產(chǎn)生目錄問(wèn)題,所以卸載代碼如下:
string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service"; Process process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "Uninstall.bat"; process.StartInfo.CreateNoWindow = true; process.Start(); System.Environment.CurrentDirectory = CurrentDirectory;
3)啟動(dòng)
代碼如下:
using System.ServiceProcess; ServiceController serviceController = new ServiceController("ServiceTest"); serviceController.Start();
4)停止
ServiceController serviceController = new ServiceController("ServiceTest"); if (serviceController.CanStop) serviceController.Stop();
5)暫停/繼續(xù)
ServiceController serviceController = new ServiceController("ServiceTest"); if (serviceController.CanPauseAndContinue) { if (serviceController.Status == ServiceControllerStatus.Running) serviceController.Pause(); else if (serviceController.Status == ServiceControllerStatus.Paused) serviceController.Continue(); }
6)檢查狀態(tài)
ServiceController serviceController = new ServiceController("ServiceTest"); string Status = serviceController.Status.ToString();
六、調(diào)試Windows Service
1)安裝并運(yùn)行服務(wù)
2)附加進(jìn)程
3)在代碼中加入斷點(diǎn)進(jìn)行調(diào)試
到此這篇關(guān)于C#創(chuàng)建Windows Service(Windows 服務(wù))的方法步驟的文章就介紹到這了,更多相關(guān)C#創(chuàng)建Windows服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#啟動(dòng)windows服務(wù)方法的相關(guān)問(wèn)題分析
- C#啟動(dòng)和停止windows服務(wù)的實(shí)例代碼
- C#編寫Windows服務(wù)實(shí)例代碼
- c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟
- c#創(chuàng)建windows服務(wù)入門教程實(shí)例
- C#使用windows服務(wù)開(kāi)啟應(yīng)用程序的方法
- C#編寫Windows服務(wù)程序詳細(xì)步驟詳解(圖文)
- C#創(chuàng)建Windows服務(wù)的實(shí)現(xiàn)方法
- C#創(chuàng)建Windows服務(wù)與服務(wù)的安裝、卸載
相關(guān)文章
C#實(shí)現(xiàn)GridView導(dǎo)出Excel實(shí)例代碼
本篇文章主要介紹了C#實(shí)現(xiàn)GridView導(dǎo)出Excel實(shí)例代碼,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03C#使用SqlConnection連接到SQL Server的代碼示例
這篇文章主要介紹了C#使用SqlConnection連接到SQL Server的代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03使用C#實(shí)現(xiàn)讀取系統(tǒng)配置文件的代碼實(shí)例講解
這篇文章主要介紹了使用C#實(shí)現(xiàn)讀取系統(tǒng)配置文件的代碼實(shí)例,使用到了ConfigurationManager類,需要的朋友可以參考下2015-12-12C#實(shí)現(xiàn)在Excel中插入和操作切片器
本文主要介紹了如何使用C#在Excel中插入和操作切片器,包括插入切片器到透視表和表格,修改切片器屬性以及刪除切片器,主要使用了Spire.XLSfor.NET庫(kù)來(lái)實(shí)現(xiàn)這些功能,需要的朋友可以參考下2025-03-03C#使用yield關(guān)鍵字讓自定義集合實(shí)現(xiàn)foreach遍歷的方法
這篇文章主要介紹了C#使用yield關(guān)鍵字讓自定義集合實(shí)現(xiàn)foreach遍歷的方法,需要的朋友可以參考下2014-08-08詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant)
這篇文章主要介紹了詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant),本文講解了協(xié)變和反變的背景知識(shí)、.NET 4.0引入的泛型協(xié)變、反變性、協(xié)變和反變的相互作用等內(nèi)容,需要的朋友可以參考下2015-06-06