C#編寫Windows服務(wù)程序詳細步驟詳解(圖文)
一、創(chuàng)建一個Windows Service
1)創(chuàng)建Windows Service項目
2)對Service重命名
將Service1重命名為你服務(wù)名稱,這里我們命名為ServiceTest。
二、創(chuàng)建服務(wù)安裝程序
1)添加安裝程序
之后我們可以看到上圖,自動為我們創(chuàng)建了ProjectInstaller.cs以及2個安裝的組件。
2)修改安裝服務(wù)名
右鍵serviceInsraller1,選擇屬性,將ServiceName的值改為ServiceTest。
3)修改安裝權(quán)限
右鍵serviceProcessInsraller1,選擇屬性,將Account的值改為LocalSystem。
三、寫入服務(wù)代碼
1)打開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."); } } } }
這里我們的邏輯很簡單,啟動服務(wù)的時候?qū)憘€日志,關(guān)閉的時候再寫個日志。
四、創(chuàng)建安裝腳本
在項目中添加2個文件如下(必須是ANSI或者UTF-8無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)安裝腳本說明
第二行為啟動服務(wù)。
第三行為設(shè)置服務(wù)為自動運行。
這2行視服務(wù)形式自行選擇。
4)腳本調(diào)試
如果需要查看腳本運行狀況,在腳本最后一行加入pause
五、在C#中對服務(wù)進行控制
0)配置目錄結(jié)構(gòu)
簡歷一個新WPF項目,叫WindowsServiceTestUI,添加對System.ServiceProcess的引用。
在WindowsServiceTestUI的bin\Debug目錄下建立Service目錄。
將WindowsServiceTest的生成目錄設(shè)置為上面創(chuàng)建的Service目錄。
生成后目錄結(jié)構(gòu)如下圖
1)安裝
安裝時會產(chǎ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)卸載
卸載時也會產(chǎ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)啟動
代碼如下:
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)安裝并運行服務(wù)
2)附加進程
3)在代碼中加入斷點進行調(diào)試
七、總結(jié)
本文對Windows service的上述配置都未做詳細解釋,但是按上述步驟就可以制作可運行的Windows Service,從而達到了工作的需求。
上一篇:C# 根據(jù)表格偶數(shù)、奇數(shù)加載不同顏色
欄 目:C#教程
本文標題:C#編寫Windows服務(wù)程序詳細步驟詳解(圖文)
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5475.html
您可能感興趣的文章
- 01-10C#實現(xiàn)自定義windows系統(tǒng)日志的方法
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10C#微信開發(fā)(服務(wù)器配置)
- 01-10輕松學習C#的正則表達式
- 01-10分享一個C#編寫簡單的聊天程序(詳細介紹)
- 01-10C# 調(diào)用 JavaWebservice服務(wù)遇到的問題匯總
- 01-10使用C#編寫簡單的圖形化的可發(fā)送附件的郵件客戶端程序
- 01-10Windows系統(tǒng)中使用C#讀取文本文件內(nèi)容的小示例
- 01-10Windows中使用C#為文件夾和文件編寫密碼鎖的示例分享


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結(jié)