Windows Service-怎么用C#編寫Windows服務程序圖文詳細說明
發表時間:2023-07-28 來源:明輝站整理相關軟件相關文章人氣:
[摘要]軟件等級:更新時間:2013-03-01金蝶桌面服務系統官方版v2.2 免費版立即下載Windows Service-如何用C#編寫Windows服務程序圖文詳解如何用C#編寫Windows服務程序...
Windows Service-如何用C#編寫Windows服務程序圖文詳解
如何用C#編寫Windows服務程序圖文詳解:Windows Service這一塊并不復雜,介紹了如何用C#創建、安裝、啟動、監控、卸載簡單的Windows Service 的內容步驟和注意事項。但是注意事項太多了,本篇不會寫復雜的東西,完全以基礎應用的需求來寫,所以不會對Windows Service寫很深入。
一、首先創建一個Windows Service
1)創建Windows Service項目


2)對Service重命名
將Service1重命名為你服務名稱,這里我們命名為ServiceTest。
二、創建服務安裝程序
1)添加安裝程序


之后我們可以看到上圖,自動為我們創建了ProjectInstaller.cs以及2個安裝的組件。
2)修改安裝服務名
右鍵serviceInsraller1,選擇屬性,將ServiceName的值改為ServiceTest。

3)修改安裝權限
右鍵serviceProcessInsraller1,選擇屬性,將Account的值改為LocalSystem。

三、寫入服務代碼
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.");
}
}
}
}
這里我們的邏輯很簡單,啟動服務的時候寫個日志,關閉的時候再寫個日志。
四、創建安裝腳本
在項目中添加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)安裝腳本說明
第二行為啟動服務。
第三行為設置服務為自動運行。
這2行視服務形式自行選擇。
4)腳本調試
如果需要查看腳本運行狀況,在腳本最后一行加入pause
五、在C#中對服務進行控制
0)配置目錄結構
簡歷一個新WPF項目,叫WindowsServiceTestUI,添加對System.ServiceProcess的引用。
在WindowsServiceTestUI的bin\Debug目錄下建立Service目錄。
將WindowsServiceTest的生成目錄設置為上面創建的Service目錄。
生成后目錄結構如下圖

1)安裝
安裝時會產生目錄問題,所以安裝代碼如下:
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)卸載
卸載時也會產生目錄問題,所以卸載代碼如下:
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)暫停/繼續
ServiceController serviceController = new ServiceController("ServiceTest");
if (serviceController.CanPauseAndContinue)
{
if (serviceController.Status == ServiceControllerStatus.Running)
serviceController.Pause();
else if (serviceController.Status == ServiceControllerStatus.Paused)
serviceController.Continue();
}
6)檢查狀態
ServiceController serviceController = new ServiceController("ServiceTest");
string Status = serviceController.Status.ToString();
六、調試Windows Service
1)安裝并運行服務
2)附加進程


3)在代碼中加入斷點進行調試

七、按上述步驟就可以制作可運行的Windows Service,從而達到了工作的需求。希望我的這個對你有所幫助!
學習教程快速掌握從入門到精通的電腦知識