c# windows服務的製作

來源:互聯網
上載者:User

標籤:引用   []   current   generic   creat   inf   component   ssi   .com   

一、建立一個Windows Service1)建立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.exeNet Start ServiceTestsc config ServiceTest start= auto2)卸載指令碼Uninstall.bat%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsServiceTest.exe3)安裝指令碼說明第二行為啟動服務。第三行為設定服務為自動運行。這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 Service1)安裝並運行服務2)附加進程3)在代碼中加入斷點進行調試七、總結本文對Windows service的上述配置都未做詳細解釋,但是按上述步驟就可以製作可啟動並執行Windows Service,從而達到了工作的需求。

原文 http://www.cr173.com/html/15350_1.html

c# windows服務的製作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.