C#編寫(xiě)Windows服務(wù)實(shí)例代碼
使用Microsoft Visual Studio2012可以很方便的創(chuàng)建一個(gè)Windows服務(wù),本例實(shí)現(xiàn)一個(gè)向D盤(pán)的txt文件里,寫(xiě)入系統(tǒng)時(shí)間的Windows服務(wù)。
新建一個(gè)Windows Services工程:
工程創(chuàng)建好之后,默認(rèn)會(huì)有一個(gè)Services1.cs文件,刪掉此文件,重新添加一個(gè)新Item
右擊新添加的這個(gè)文件,選擇View code,可以看到,有兩個(gè)函數(shù) OnStart和OnStop,OnStart函數(shù)在啟動(dòng)服務(wù)時(shí)執(zhí)行,OnStop函數(shù)在停止服務(wù)時(shí)執(zhí)行。
這兩個(gè)函數(shù)的代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace MyFirstWindowsService
{
partial class MyWindowsService : ServiceBase
{
public MyWindowsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
FileStream fileStream = new FileStream(@"D:\MyWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.BaseStream.Seek(0, SeekOrigin.End);
streamWriter.WriteLine("My service started" + DateTime.Now.ToString() + "\n");
streamWriter.Flush();
streamWriter.Close();
fileStream.Close();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
FileStream fileStream = new FileStream(@"D:\MyWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.BaseStream.Seek(0, SeekOrigin.End);
streamWriter.WriteLine("My service stopped " + DateTime.Now.ToString() + "\n");
streamWriter.Flush();
streamWriter.Close();
fileStream.Close();
}
}
}
之后需要新建一個(gè)安裝組件MyWindowsServiceProjectInstaller(右擊MyWindowsService.cs這個(gè)文件選擇view desiner,然后選擇Add Installer),需要將MyFirstWindowsServiceProcessInstaller的account屬性設(shè)置為localservice.
編寫(xiě)批處理文件:
安裝服務(wù)批處理:
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe C:\Users\gaoja1\Desktop\MyWindowsService\MyFirstWindowsService\bin\Debug\MyFirstWindowsService.exe
Net Start ServiceTest
sc config ServiceTest start= auto
卸載服務(wù)批處理:
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u C:\Users\gaoja1\Desktop\MyWindowsService\MyFirstWindowsService\bin\Debug\MyFirstWindowsService.exe
服務(wù)安裝好之后:

服務(wù)啟動(dòng)之后可以在D盤(pán)看到一個(gè)txt的文件,里面記錄了服務(wù)的啟動(dòng)時(shí)間.
- C#啟動(dòng)windows服務(wù)方法的相關(guān)問(wèn)題分析
- C#啟動(dòng)和停止windows服務(wù)的實(shí)例代碼
- c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟
- c#創(chuàng)建windows服務(wù)入門(mén)教程實(shí)例
- C#使用windows服務(wù)開(kāi)啟應(yīng)用程序的方法
- C#編寫(xiě)Windows服務(wù)程序詳細(xì)步驟詳解(圖文)
- C#創(chuàng)建Windows服務(wù)的實(shí)現(xiàn)方法
- C#創(chuàng)建Windows服務(wù)與服務(wù)的安裝、卸載
- C#創(chuàng)建Windows Service(Windows 服務(wù))的方法步驟
相關(guān)文章
C#時(shí)間格式轉(zhuǎn)換為時(shí)間戳的方法步驟
這篇文章主要介紹了C#時(shí)間格式轉(zhuǎn)換為時(shí)間戳的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02C# TextBox控件實(shí)現(xiàn)只能輸入數(shù)字的方法
這篇文章主要介紹了C# TextBox控件實(shí)現(xiàn)只能輸入數(shù)字的方法,本文使用TextBox的keypress事件實(shí)現(xiàn)這個(gè)需求,需要的朋友可以參考下2015-06-06C#中Thread(線(xiàn)程)和Task(任務(wù))實(shí)例詳解
.NET Framework在System.Threading命名空間中具有與線(xiàn)程相關(guān)的類(lèi),線(xiàn)程是一小組可執(zhí)行指令,這篇文章主要給大家介紹了關(guān)于C#中Thread(線(xiàn)程)和Task(任務(wù))的相關(guān)資料,需要的朋友可以參考下2022-03-03C#調(diào)用百度翻譯實(shí)現(xiàn)翻譯HALCON的示例
HALCON示例程序的描述部分一直是英文的,看起來(lái)很不方便。本文就使用百度翻譯實(shí)現(xiàn)翻譯HALCON,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C#配置文件Section節(jié)點(diǎn)處理總結(jié)
這篇文章主要介紹了C#配置文件Section節(jié)點(diǎn)處理總結(jié),針對(duì)配置文件Section節(jié)點(diǎn)的處理做了較為詳細(xì)的實(shí)例總結(jié),需要的朋友可以參考下2014-10-10