C#創建一個Window服務
阿新 • • 發佈:2018-05-29
public 自動啟動 com efi 計算機 component 工作 使用 ica
Window服務介紹
Microsoft Windows 服務能夠創建在它們自己的 Windows 會話中可長時間運行的可執行應用程序。這些服務可以在計算機啟動時自動啟動,可以暫停和重新啟動而且不顯示任何用戶界面。這使服務非常適合在服務器上使用,或任何時候,為了不影響在同一臺計算機上工作的其他用戶,需要長時間運行功能時使用。還可以在不同於登錄用戶的特定用戶帳戶或默認計算機帳戶的安全上下文中運行服務。本文就向大家介紹如何運用Visual C#來一步一步創建一個文件監視的Windows服務程序,然後介紹如何安裝、測試和調試該Windows服務程序。
1.創建window服務
創建完成後發現此應用程序的入口
static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new callmeyhzService() }; ServiceBase.Run(ServicesToRun); }
修改服務名稱
編寫服務代碼
public partial class callmeyhzService : ServiceBase {public callmeyhzService() { InitializeComponent(); } string filePath = @"D:\MyServiceLog.txt"; protected override void OnStart(string[] args) { using (FileStream stream = new FileStream(filePath,FileMode.Append)) using(StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss" + "服務啟動!")); } } protected override void OnStop() { using (FileStream stream = new FileStream(filePath, FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss" + "服務停止!")); } } }
此時如果直接執行服務是無法安裝的
2.寫一個桌面應用程序管理服務
最終我們希望window服務應該在service.msc中存在
編寫一個winform就放4個按鈕
上代碼
using System; using System.Collections; using System.Windows.Forms; using System.ServiceProcess; using System.Configuration.Install; namespace WindowServiceClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string serviceFilePath = Application.StartupPath + @"\CallmeYhz.exe"; string serviceName = "花生的服務"; //判斷服務是否存在 private bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { return true; } } return false; } //卸載服務 private void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } //安裝服務 private void InstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; IDictionary savedState = new Hashtable(); installer.Install(savedState); installer.Commit(savedState); } } //啟動服務 private void ServiceStart(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Stopped) { control.Start(); } } } //停止服務 private void ServiceStop(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } } /// <summary> /// 窗體加載事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { } #region 按鈕事件 /// <summary> /// 安裝服務按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Setep_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); } /// <summary> /// 啟動按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void start_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName); } /// <summary> /// 停止按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void stop_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName); } /// <summary> /// 卸載按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Union_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); } } #endregion } }
先安裝服務再運行服務再停止服務再寫在服務
服務已經成功安裝並且啟動:
觀察日誌
說明本次小demo圓滿完成
C#創建一個Window服務