日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁編程開發(fā)C#.NET → C#編寫Windows服務(wù)程序圖文教程

      C#編寫Windows服務(wù)程序圖文教程

      相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2012/5/16 12:03:15字體大小:A-A+

      作者:佚名點(diǎn)擊:3846次評論:0次標(biāo)簽: Windows服務(wù)

      Windows服務(wù)管理器1.2.5G綠色免費(fèi)版
      • 類型:系統(tǒng)優(yōu)化大小:20KB語言:中文 評分:5.0
      • 標(biāo)簽:
      立即下載

      Windows Service這一塊并不復(fù)雜,但是注意事項(xiàng)太多了,網(wǎng)上資料也很凌亂,偶爾自己寫也會丟三落四的。所以本文也就產(chǎn)生了,本文不會寫復(fù)雜的東西,完全以基礎(chǔ)應(yīng)用的需求來寫,所以不會對Windows Service寫很深入。

      本文介紹了如何用C#創(chuàng)建、安裝、啟動、監(jiān)控、卸載簡單的Windows Service 的內(nèi)容步驟和注意事項(xiàng)。

      一、創(chuàng)建一個Windows Service

      1)創(chuàng)建Windows Service項(xiàng)目

      2)對Service重命名

      將Service1重命名為你服務(wù)名稱,這里我們命名為ServiceTest。

      二、創(chuàng)建服務(wù)安裝程序

      1)添加安裝程序

      之后我們可以看到上圖,自動為我們創(chuàng)建了ProjectInstaller.cs以及2個安裝的組件。

      2)修改安裝服務(wù)名

      右鍵serviceInsraller1,選擇屬性,將ServiceName的值改為ServiceTest。

      3)修改安裝權(quán)限

      右鍵serviceProcessInsraller1,選擇屬性,將Account的值改為LocalSystem。

      三、寫入服務(wù)代碼

      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.");
      			}
      		}
      	}
      }
      

      這里我們的邏輯很簡單,啟動服務(wù)的時候?qū)憘日志,關(guān)閉的時候再寫個日志。

      四、創(chuàng)建安裝腳本

      在項(xiàng)目中添加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)安裝腳本說明

      第二行為啟動服務(wù)。

      第三行為設(shè)置服務(wù)為自動運(yùn)行。

      這2行視服務(wù)形式自行選擇。

      4)腳本調(diào)試

      如果需要查看腳本運(yùn)行狀況,在腳本最后一行加入pause

      五、在C#中對服務(wù)進(jìn)行控制

      0)配置目錄結(jié)構(gòu)

      簡歷一個新WPF項(xiàng)目,叫WindowsServiceTestUI,添加對System.ServiceProcess的引用。

      在WindowsServiceTestUI的bin\Debug目錄下建立Service目錄。

      將WindowsServiceTest的生成目錄設(shè)置為上面創(chuàng)建的Service目錄。

      生成后目錄結(jié)構(gòu)如下圖

      1)安裝

      安裝時會產(chǎn)生目錄問題,所以安裝代碼如下:

      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)卸載

      卸載時也會產(chǎn)生目錄問題,所以卸載代碼如下:

      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)暫停/繼續(xù)

      ServiceController serviceController = new ServiceController("ServiceTest");
      if (serviceController.CanPauseAndContinue)
      {
      	if (serviceController.Status == ServiceControllerStatus.Running)
      		serviceController.Pause();
      	else if (serviceController.Status == ServiceControllerStatus.Paused)
      		serviceController.Continue();
      }

      6)檢查狀態(tài)

      ServiceController serviceController = new ServiceController("ServiceTest");
      string Status = serviceController.Status.ToString();
      

      六、調(diào)試Windows Service

      1)安裝并運(yùn)行服務(wù)

      2)附加進(jìn)程

      3)在代碼中加入斷點(diǎn)進(jìn)行調(diào)試

      七、總結(jié)

      本文對Windows service的上述配置都未做詳細(xì)解釋,但是按上述步驟就可以制作可運(yùn)行的Windows Service,從而達(dá)到了工作的需求。

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        第 5 樓 河北張家口張家口教育學(xué)院 網(wǎng)友 客人 發(fā)表于: 2014/5/15 20:33:08
        安裝代碼寫到哪里?

        支持( 0 ) 蓋樓(回復(fù))

        第 4 樓 美國CZ88.NET 網(wǎng)友 客人 發(fā)表于: 2014/3/17 13:10:29
        沒太看明白,我想用服務(wù)來做些事情,應(yīng)該寫到哪里?

        支持( 0 ) 蓋樓(回復(fù))

        第 3 樓 1 網(wǎng)友 客人 發(fā)表于: 2013/5/30 13:09:29
        不錯!適合入門學(xué)習(xí)

        支持( 0 ) 蓋樓(回復(fù))

        第 2 樓 江蘇揚(yáng)州揚(yáng)州工業(yè)職業(yè)技術(shù)學(xué)院 網(wǎng)友 客人 發(fā)表于: 2013/5/8 9:15:54
        很好

        支持( 0 ) 蓋樓(回復(fù))

        第 1 樓 江蘇(全通用) 網(wǎng)友 客人 發(fā)表于: 2013/3/21 10:55:19
        寫的很好 只是看了半天才看懂

        支持( 0 ) 蓋樓(回復(fù))

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)