資源簡(jiǎn)介
附件中有詳細(xì)的安裝使用文檔,大概步驟如下:
1.新建Windows項(xiàng)目,選擇"Windows服務(wù)"類型的工程。
2.生成的Program.cs文件中,定義了服務(wù)啟動(dòng)的Main函數(shù)。
代碼
namespace WindowsService1
{
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
3.在新建的工程中,點(diǎn)擊Service1.cs文件,切換到代碼視圖,生成的代碼繼承于ServiceBase基類,并重載了OnStart和OnStop方法。我在這個(gè)文件中進(jìn)行了一些簡(jiǎn)單的操作,就是在服務(wù)開(kāi)始的時(shí)候,定義一個(gè)定時(shí)器,然后每隔1秒鐘,向文件中寫入當(dāng)前時(shí)間。
代碼
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
Timer timer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer = new Timer(1000);
timer.Elapsed = new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
timer.Dispose();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
string filePath = AppDomain.CurrentDomain.BaseDirectory "test.txt";
StreamWriter sw = null;
if (!File.Exists(filePath))
{
sw = File.CreateText(filePath);
}
else
{
sw = File.AppendText(filePath);
}
sw.Write("訪問(wèn)時(shí)間:" DateTime.Now.ToString() Environment.NewLine);
sw.Close();
}
}
}
4.向工程中添加一個(gè)安裝程序類。
4.在新添加的安裝程序類中,設(shè)定服務(wù)的名稱,啟動(dòng)方式,賬號(hào)名和密碼等信息。
代碼
namespace WindowsService1
{
partial class Installer1
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 組件設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// 創(chuàng)建ServiceProcessInstaller對(duì)象和ServiceInstaller對(duì)象
this.spInstaller =new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();
// 設(shè)定ServiceProcessInstaller對(duì)象的帳號(hào)、用戶名和密碼等信息
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Password = null;
this.spInstaller.Username = null;
// 設(shè)定服務(wù)的名稱
this.sInstaller.ServiceName = "WindowsService1";
//設(shè)定服務(wù)啟動(dòng)的方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.spInstaller,this.sInstaller});
}
#endregion
}
}
5.生成工程,在bin目錄下會(huì)生成exe文件。如果直接運(yùn)行exe文件的話,是不能執(zhí)行的,需要使用安裝Windows服務(wù)用到一個(gè)名為InstallUtil.exe的命令行工具,打開(kāi)命令行工具,轉(zhuǎn)到InstallUtil.exe的目錄下,我安裝的是VS 2010,對(duì)應(yīng)的目錄為:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe,然后執(zhí)行InstallUtil.exe 待執(zhí)行的exe文件的目錄,如:InstallUtil.exe F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe。執(zhí)行成功后,會(huì)在Windows的服務(wù)中,出現(xiàn)了剛剛添加的服務(wù)的名稱。
6.啟動(dòng)該服務(wù),這時(shí)打開(kāi)bin\Debug文件夾,發(fā)現(xiàn)已經(jīng)生成了一個(gè)test.txt的文件,里面記錄了時(shí)間。這說(shuō)明服務(wù)已經(jīng)正式開(kāi)始執(zhí)行。
7.停止服務(wù)的操作也和簡(jiǎn)單,打開(kāi)命令行工具,轉(zhuǎn)到C:\Windows\Microsoft.NET\Framework\v4.0.30319目錄,然后執(zhí)行InstallUtil.exe - u F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe命令就可以了。
代碼片段和文件信息
using?System;
using?System.Collections;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Configuration.Install;
using?System.Linq;
namespace?WindowsService1
{
????[RunInstaller(true)]
????public?partial?class?Installer1?:?System.Configuration.Install.Installer
????{
????????public?Installer1()
????????{
????????????InitializeComponent();
????????}
????}
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????1050??2011-01-13?14:36??WindowsService1\WindowsService1\bin\Debug\test.txt
?????文件???????6656??2011-01-13?14:33??WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe
?????文件??????14739??2011-01-13?14:36??WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog
?????文件??????19968??2011-01-13?14:33??WindowsService1\WindowsService1\bin\Debug\WindowsService1.pdb
?????文件??????11600??2011-01-13?14:33??WindowsService1\WindowsService1\bin\Debug\WindowsService1.vshost.exe
?????文件????????490??2010-03-17?22:39??WindowsService1\WindowsService1\bin\Debug\WindowsService1.vshost.exe.manifest
?????文件????????409??2011-01-10?15:34??WindowsService1\WindowsService1\Installer1.cs
?????文件???????2082??2011-01-10?16:27??WindowsService1\WindowsService1\Installer1.Designer.cs
?????文件???????5217??2011-01-11?10:09??WindowsService1\WindowsService1\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6148??2011-01-13?14:33??WindowsService1\WindowsService1\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????8080??2011-01-13?14:33??WindowsService1\WindowsService1\obj\x86\Debug\ResolveAssemblyReference.cache
?????文件???????1457??2011-01-13?14:33??WindowsService1\WindowsService1\obj\x86\Debug\WindowsService1.csproj.FileListAbsolute.txt
?????文件???????6656??2011-01-13?14:33??WindowsService1\WindowsService1\obj\x86\Debug\WindowsService1.exe
?????文件??????19968??2011-01-13?14:33??WindowsService1\WindowsService1\obj\x86\Debug\WindowsService1.pdb
?????文件????????509??2011-01-10?15:32??WindowsService1\WindowsService1\Program.cs
?????文件???????1376??2011-01-10?15:32??WindowsService1\WindowsService1\Properties\AssemblyInfo.cs
?????文件???????1360??2011-01-13?14:28??WindowsService1\WindowsService1\Service1.cs
?????文件???????1090??2011-01-10?15:32??WindowsService1\WindowsService1\Service1.Designer.cs
?????文件???????3018??2011-01-13?14:32??WindowsService1\WindowsService1\WindowsService1.csproj
?????文件????????887??2011-01-13?14:28??WindowsService1\WindowsService1.sln
????..A..H.?????33280??2011-01-13?14:33??WindowsService1\WindowsService1.suo
?????目錄??????????0??2011-01-13?14:33??WindowsService1\WindowsService1\obj\x86\Debug\TempPE
?????目錄??????????0??2011-01-13?14:33??WindowsService1\WindowsService1\obj\x86\Debug
?????目錄??????????0??2011-01-13?14:36??WindowsService1\WindowsService1\bin\Debug
?????目錄??????????0??2011-01-13?14:33??WindowsService1\WindowsService1\obj\x86
?????目錄??????????0??2011-01-13?14:33??WindowsService1\WindowsService1\bin
?????目錄??????????0??2011-01-13?14:33??WindowsService1\WindowsService1\obj
?????目錄??????????0??2011-01-13?14:33??WindowsService1\WindowsService1\Properties
?????目錄??????????0??2011-01-13?14:33??WindowsService1\WindowsService1
?????目錄??????????0??2011-01-13?14:33??WindowsService1
............此處省略4個(gè)文件信息
評(píng)論
共有 條評(píng)論