Windows服務程式圖文教程
Windows服務程式圖文教程
1、新建服務專案
在Visual C# 下的選擇WindowsService,輸入專案名稱,點選【OK】
將類名修改為WriteLog.cs

2、編寫服務程式碼
右鍵點選WriteLog.cs,選擇View Code(檢視程式碼)
服務程式碼中需要重寫兩個方法:OnStart、OnStop,其中OnStart方法供服務啟動時呼叫,OnStop方法供服務停止時呼叫。
程式碼如下:
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 MyService
{
public partial class WriteLog : ServiceBase
{
public WriteLog()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("D:\\log.txt", true);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Service WriteLog Start!");
sw.Flush();
sw.Close();
}
protected override void OnStop()
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("D:\\log.txt", true);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Service WriteLog Stop!");
sw.Flush();
sw.Close();
}
}
}
程式的目的是在服務啟動時向D盤下的log.txt檔案(若沒有這個檔案則建立)中寫入一條啟動日誌,服務停止時追加寫入一條停止日誌
3、為服務新增安裝程式
雙擊“WriteLog.cs”,VS中間將出現服務的設計區域

右鍵選擇“AddInstaller”

出現serviceProcessInstaller1和serviceInstaller1(如果專案中有兩個服務的話,就會出現兩個serviceInstaller)

下圖是解決方案瀏覽器的結構:

然後在serviceProcessInstaller1的屬性頁中指定服務的許可權
這裡選擇LocalSystem,本地系統,這是服務的最高許可權
在serviceInstaller1的屬性頁中填入服務的DisplayName(顯示名稱,即顯示在Windows服務管理器中的名稱),Description(服務的描述),ServiceName(服務的內部名稱),同時指定服務的啟動方式,分為Automatic(自動)、Manual(手動)、Disabled(禁用)。

這裡選擇Automatic,即開機啟動
4、編譯
右鍵點選MyService專案,選擇Build即可編譯

編譯完成後,在專案的Debug資料夾裡,即可看到服務的exe可執行程式

習慣性雙擊MyService.exe,會出現一個“Windows Service Start Failure”的提示

原來服務程式必須先用installutil.exe進行安裝,然後在服務管理器中啟動才能使用。
5、安裝服務程式
進入CMD控制檯,在.NET路徑下找到installutil.exe

將服務程式的路徑作為installutil.exe的輸入引數傳入(可以直接將檔案拖進命令列即可出現絕對路徑)並回車

服務安裝成功。
右鍵點選“我的電腦”——>“管理”,在出現的“計算機管理”對話方塊中選擇“服務和應用程式”——>“服務”

視窗右邊即可以找到我們剛才已經安裝的服務
可以看到現在服務的狀態為空白,說明服務還未啟動。右鍵點選安裝的服務,選擇“啟動”,即可啟動服務
啟動後我們在D盤下找到了一個log.txt檔案

手動停止服務,則在log.txt檔案中有新增了一行日誌。
明白了這個過程,就可以寫一個批處理檔案,雙擊即可執行安裝服務程式而不需要手動在命令列中輸入。
在MyService.exe所在路徑下新建一個install.txt
內容如下:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installUtil.exe MyService.exe
NetStart WriteLog
將字尾名改為.bat
雙擊即可執行服務的安裝並啟動服務
6、解除安裝服務
與安裝服務類似,只是在InstallUtil.exe 後面,服務程式所在路徑的前面加上引數 /u,回車即可。在服務管理中重新整理下即可發現WriteLog服務不在了。

同樣也可以編寫一個批處理檔案解除安裝服務
在MyService.exe所在路徑下新建一個uninstall.txt
內容如下:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installUtil.exe/u MyService.exe
將字尾名改為.bat
雙擊即可執行服務的解除安裝
上面我們用命令列的形式來安裝服務程式,這個過程顯得較為繁瑣,不利於使用者的操作。其實windows服務專案還可以做成安裝程式的形式來安裝,這樣就比較方便了。下一篇我們就來介紹為windows服務專案新增安裝程式。
相關文章
- windows開啟ssh服務教程Windows
- Windows 服務設定工具 WinSW 使用教程Windows
- PHP 程式跑在 Windows 服務上PHPWindows
- 將windows應用程式註冊為windows服務Windows
- windows 服務執行啟動桌面程式Windows
- windows8.1升級windows10預覽版詳細教程【圖文】Windows
- Windows 服務管理Windows
- windows驅動註冊中斷服務程式Windows
- C# Windows Service 服務程式的編寫C#Windows
- exe程式註冊成windows系統服務Windows
- Windows刪除服務Windows
- win10系統如何配置telnet服務【圖文】Win10
- win10系統如何新增iis服務【圖文】Win10
- Windows Server 2019伺服器系統安裝教程圖文詳解WindowsServer伺服器
- 使用C#建立windows服務續之使用Topshelf優化Windows服務C#Windows優化
- 使用C#建立安裝Windows服務程式(乾貨)C#Windows
- C#開發一個混合Windows服務和Windows窗體的程式C#Windows
- windows 安裝Nginx服務WindowsNginx
- 遠端管理Windows伺服器上的IIS服務的方法教程Windows伺服器
- windows server 2008 r2 DNS伺服器配置教程圖文詳解WindowsServerDNS伺服器
- linux系統安裝MySQL服務,詳細圖文教程LinuxMySql
- Kali Linux常用服務配置教程DHCP服務原理Linux
- 程式設計師必備技能:一鍵建立windows 服務程式設計師Windows
- 使用InstallUtil釋出windows服務Windows
- Windows關閉Microsoft Defender服務WindowsROS
- windows下安裝Tomcat服務WindowsTomcat
- C# 註冊Windows服務C#Windows
- 使用.NET Core建立Windows服務Windows
- windows下啟動nacos服務Windows
- KaliLinux常用服務配置教程DHCP服務工作流程Linux
- Kali Linux常用服務配置教程啟動DHCP服務Linux
- Django新手圖文入門教程Django
- PopClip使用教程圖文詳解
- Anaconda 的安裝教程(圖文)
- 微信小程式新增外部地圖服務資料微信小程式地圖
- 譯文|藉助 Pulsar Functions 遷移到無服務應用程式Function
- windows系統服務配置詳解,以及開發好的windows服務怎麼部署上去Windows
- Windows如何安裝自定義服務Windows
- Windows遠端連線Docker服務WindowsDocker