在 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
沒有留言 :
張貼留言