2012-09-22

使用 Visual Studio 建立 Windows Service (Create a Windows Service with Visual Studio)

打算設置一個開機自動啟動程序,使用者通常會將檔案或其捷徑存放至自動啟動目錄
在 Windows 自動啟動目錄存放於 開始 > 所有程式 > 啟動

但不同的使用者會有不同設定,以另一個使用者帳戶登入 Windows,他未必會啟動同一個檔案

Windows 能夠觸發在起動時執行背景程序
這些程序在使用者登入前已經執行,它們稱為 Windows Service


首先,你先安裝 Visual Studio
見下文
點擊 File > New > Project 開啟新專案

見下文
展開 Visual C# (閣下可以使用其他語言,但這篇文章是使用 C#)
及選擇 Windows Service Visual C#
然後設定專案名,然後按 OK 建立專案

見下文
Visual Studio 會這樣顯示
假如 Service1.cs [Design] 沒有顯示,在 Solution Explorer 雙點擊 Service1.cs

見下文
在 Service1.cs 點擊 code view 會自動建立兩個空方法,分別是
protected override void OnStart(string[] args)
這個方法是當 service 啟動時執行

protected override void OnStop()
這個方法是當 service 停止時執行

閣下可以覆寫其他方法,可以到 MSDN 尋找其他方法
圖中的編程只是例子
注意:Windows Service 不能使用任何具介面的函式庫

見下文
Back to the Service1.cs [Design]
在 Design Form 右點擊任何空白位置,然後按 Add Installer

見下文
建立 ProjectInstaller.cs [Design]
Click the serviceInstaller1 then right click it and click Properties (If you expand the properties window, you don't need to to it)
Modify ServiceName And StartType for your service (I suggest you don't use special character including <space>)
Automatic means when you booted the system, the service will start automaticlly
Manual means you should start the service manually
Disabled means you should enable the service before you would to start it

見下文
Click the serviceProcessInstaller1 then right click it and click Properties (If you expand the properties window, you don't need to to it)
Modify account to LocalSystem

見下文
Right click your service project and click Properties

見下文
On the properties dialog, change Startup object to your service program

見下文
Right click your solution Add > New Project

見下文
Expand Other Object Type > Setup and Development, click Setup Project
And name the setup project in Name field
Then click OK

見下文
Right click your setup project Add > Project Output

見下文
Select your service project in Project and select Primary
And (Active) in Configure
Then click OK

見下文
Right click your setup project View > Custom Actions

見下文
In Custom Actions form, right click Custom Actions and click Add Custom Action

見下文
Double click the Application Folder

見下文
Select Project Output from <your service project> (Active) then click OK

見下文
After you finished the actions before, the form presents like this

見下文
Right click your service project and click build
If you have built the service, you should click rebuild

見下文
Right click your setup project and click build
If you have built the setup, you should click rebuild

見下文
Access to the setup project directory the setup.exe file is located at debug
directory


見下文
見下文
見下文
見下文
見下文
見下文
Install the service

見下文
If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the installation process

見下文
Open the command line mode

見下文
If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the installation process

見下文
type
net start "<your service name>"
to start your service
You also can start your service in Control Panel > Administrative Tools > Services

見下文
The result represent the action when started this service

見下文
type
net stop "<your service name>"
to start your service
You also can stop your service in Control Panel > Administrative Tools > Services

見下文
The result represent the action when stopped this service

見下文
Goto Control Panel > Uninstall a Program

見下文
Find out your service setup

見下文
見下文
見下文
Click Uninstall at the center to begin the uninstall wizard

見下文
If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the uninstallation process

沒有留言 :

張貼留言