日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

(转)创建Windows服务(Windows Services)N种方式总结

發(fā)布時間:2025/6/15 windows 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)创建Windows服务(Windows Services)N种方式总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)自:http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html

最近由于工作需要,寫了一些windows服務(wù)程序,有一些經(jīng)驗,我現(xiàn)在總結(jié)寫出來。
目前我知道的創(chuàng)建創(chuàng)建Windows服務(wù)有3種方式:
a.利用.net框架類ServiceBase
b.利用組件Topshelf
c.利用小工具instsrv和srvany

下面我利用這3種方式,分別做一個windows服務(wù)程序,程序功能就是每隔5秒往程序目錄下記錄日志:

?

a.利用.net框架類ServiceBase

本方式特點:簡單,兼容性好

通過繼承.net框架類ServiceBase實現(xiàn)

第1步: 新建一個Windows服務(wù)

public partial class Service1 : ServiceBase{readonly Timer _timer;private static readonly string FileName = Path.GetDirectoryName ( Assembly.GetExecutingAssembly ( ).Location ) + @"\" + "test.txt";public Service1 ( ){InitializeComponent ( );_timer = new Timer ( 5000 ){AutoReset = true ,Enabled = true};_timer.Elapsed += delegate ( object sender , ElapsedEventArgs e ){this.witre ( string.Format ( "Run DateTime {0}" , DateTime.Now ) );};}protected override void OnStart ( string [ ] args ){this.witre ( string.Format ( "Start DateTime {0}" , DateTime.Now ) );}protected override void OnStop ( ){this.witre ( string.Format ( "Stop DateTime {0}" , DateTime.Now ) + Environment.NewLine );}void witre ( string context ){StreamWriter sw = File.AppendText ( FileName );sw.WriteLine ( context );sw.Flush ( );sw.Close ( );}}

第2步: 添加Installer

[RunInstaller ( true )]public partial class Installer1 : System.Configuration.Install.Installer{private ServiceInstaller serviceInstaller;private ServiceProcessInstaller processInstaller;public Installer1 ( ){InitializeComponent ( );processInstaller = new ServiceProcessInstaller ( );serviceInstaller = new ServiceInstaller ( );processInstaller.Account = ServiceAccount.LocalSystem;serviceInstaller.StartType = ServiceStartMode.Automatic;serviceInstaller.ServiceName = "my_WindowsService";serviceInstaller.Description = "WindowsService_Description";serviceInstaller.DisplayName = "WindowsService_DisplayName";Installers.Add ( serviceInstaller );Installers.Add ( processInstaller );} }

第3步:安裝,卸載?
Cmd命令
installutil????? WindowsService_test.exe? (安裝Windows服務(wù))
installutil /u?? WindowsService_test.exe? (卸載Windows服務(wù))

代碼下載:http://files.cnblogs.com/aierong/WindowsService_test.rar

?

b.利用組件Topshelf

本方式特點:代碼簡單,開源組件,Windows服務(wù)可運行多個實例

Topshelf是一個開源的跨平臺的服務(wù)框架,支持Windows和Mono,只需要幾行代碼就可以構(gòu)建一個很方便使用的服務(wù). 官方網(wǎng)站:http://topshelf-project.com

?

第1步:引用程序集TopShelf.dll和log4net.dll

第2步:創(chuàng)建一個服務(wù)類MyClass,里面包含兩個方法Start和Stop,還包含一個定時器Timer,每隔5秒往文本文件中寫入字符

public class MyClass{readonly Timer _timer;private static readonly string FileName = Directory.GetCurrentDirectory ( ) + @"\" + "test.txt";public MyClass ( ){_timer = new Timer ( 5000 ){AutoReset = true ,Enabled = true};_timer.Elapsed += delegate ( object sender , ElapsedEventArgs e ){this.witre ( string.Format ( "Run DateTime {0}" , DateTime.Now ) );};}void witre ( string context ){StreamWriter sw = File.AppendText ( FileName );sw.WriteLine ( context );sw.Flush ( );sw.Close ( );}public void Start ( ){this.witre ( string.Format ( "Start DateTime {0}" , DateTime.Now ) );}public void Stop ( ){this.witre ( string.Format ( "Stop DateTime {0}" , DateTime.Now ) + Environment.NewLine );}}

第3步:使用Topshelf宿主我們的服務(wù),主要是Topshelf如何設(shè)置我們的服務(wù)的配置和啟動和停止的時候的方法調(diào)用

class Program{static void Main ( string [ ] args ){HostFactory.Run ( x =>{x.Service<MyClass> ( ( s ) =>{s.SetServiceName ( "ser" );s.ConstructUsing ( name => new MyClass ( ) );s.WhenStarted ( ( t ) => t.Start ( ) );s.WhenStopped ( ( t ) => t.Stop ( ) );} );x.RunAsLocalSystem ( );//服務(wù)的描述x.SetDescription ( "Topshelf_Description" );//服務(wù)的顯示名稱x.SetDisplayName ( "Topshelf_DisplayName" );//服務(wù)名稱x.SetServiceName ( "Topshelf_ServiceName" );} );}}

第4步: cmd命令

ConsoleApp_Topshelf.exe? install??? (安裝Windows服務(wù))

ConsoleApp_Topshelf.exe? uninstall? (卸載Windows服務(wù))

?

代碼下載:http://files.cnblogs.com/aierong/ConsoleApp_Topshelf.rar

?

c.利用小工具instsrv和srvany

本方式特點:代碼超級簡單,WindowsForm程序即可,并支持程序交互(本人最喜歡的特點),好像不支持win7,支持xp win2003

首先介紹2個小工具:

instsrv.exe:用以安裝和卸載可執(zhí)行的服務(wù)

srvany.exe:用于將任何EXE程序作為Windows服務(wù)運行

?

這2個工具都是是Microsoft Windows Resource Kits工具集的實用的小工具?

你可以通過下載并安裝Microsoft Windows Resource Kits獲得?http://www.microsoft.com/en-us/download/details.aspx?id=17657

?

第1步: 新建WindowsForm程序

public partial class Form1 : Form{Timer _timer;private static readonly string FileName = Application.StartupPath + @"\" + "test.txt";public Form1 ( ){InitializeComponent ( );}private void Form1_Load ( object sender , EventArgs e ){_timer = new Timer ( ){Enabled = true ,Interval = 5000};_timer.Tick += delegate ( object _sender , EventArgs _e ){this.witre ( string.Format ( "Run DateTime {0}" , DateTime.Now ) );};}void _timer_Tick ( object sender , EventArgs e ){throw new NotImplementedException ( );}void witre ( string context ){StreamWriter sw = File.AppendText ( FileName );sw.WriteLine ( context );sw.Flush ( );sw.Close ( );}private void button1_Click ( object sender , EventArgs e ){MessageBox.Show ( "Hello" );}}

?

?

第2步:安裝,卸載

服務(wù)的安裝步驟分5小步:

(1)打開CMD,輸入以下內(nèi)容,其中WindowsForms_WindowsService為你要創(chuàng)建的服務(wù)名稱

格式:目錄絕對路徑\instsrv ?WindowsForms_WindowsService? 目錄絕對路徑\srvany.exe

例如:

D:\TempWork\win\Debug\instsrv.exe ?WindowsForms_WindowsService ?D:\TempWork\win\Debug\srvany.exe

?

(2)regedit打開注冊表編輯器,找到以下目錄

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WindowsForms_WindowsService

?

(3)鼠標(biāo)右鍵單擊WindowsForms_WindowsService,創(chuàng)建一個"項",名稱為"Parameters"

?

(4)鼠標(biāo)左鍵單擊"Parameters",在右邊點擊鼠標(biāo)右鍵,創(chuàng)建一個"字符串值"(REG_SZ),名稱為"Application",數(shù)值數(shù)據(jù)里填寫目錄下可執(zhí)行文件的絕對路徑+文件名

例如:

D:\TempWork\win\Debug\WindowsFormsApplication_Exe.exe

?

(5)打開services.msc服務(wù)控制面板,找到WindowsForms_WindowsService服務(wù),鼠標(biāo)右鍵-屬性-登陸,勾選"允許服務(wù)與桌面交互"

?

啟動服務(wù),可以看到程序界面

?


?

卸載服務(wù)

D:\TempWork\win\Debug\instsrv.exe WindowsForms_WindowsService REMOVE

?

代碼下載:http://files.cnblogs.com/aierong/WindowsFormsApplication_Exe.rar

?

總結(jié)

以上是生活随笔為你收集整理的(转)创建Windows服务(Windows Services)N种方式总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。