資源簡介
附件中有詳細的安裝使用文檔,大概步驟如下:
1.新建Windows項目,選擇"Windows服務"類型的工程。
2.生成的Program.cs文件中,定義了服務啟動的Main函數。
代碼
namespace WindowsService1
{
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
3.在新建的工程中,點擊Service1.cs文件,切換到代碼視圖,生成的代碼繼承于ServiceBase基類,并重載了OnStart和OnStop方法。我在這個文件中進行了一些簡單的操作,就是在服務開始的時候,定義一個定時器,然后每隔1秒鐘,向文件中寫入當前時間。
代碼
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("訪問時間:" DateTime.Now.ToString() Environment.NewLine);
sw.Close();
}
}
}
4.向工程中添加一個安裝程序類。
4.在新添加的安裝程序類中,設定服務的名稱,啟動方式,賬號名和密碼等信息。
代碼
namespace WindowsService1
{
partial class Installer1
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 組件設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// 創建ServiceProcessInstaller對象和ServiceInstaller對象
this.spInstaller =new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();
// 設定ServiceProcessInstaller對象的帳號、用戶名和密碼等信息
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Password = null;
this.spInstaller.Username = null;
// 設定服務的名稱
this.sInstaller.ServiceName = "WindowsService1";
//設定服務啟動的方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.spInstaller,this.sInstaller});
}
#endregion
}
}
5.生成工程,在bin目錄下會生成exe文件。如果直接運行exe文件的話,是不能執行的,需要使用安裝Windows服務用到一個名為InstallUtil.exe的命令行工具,打開命令行工具,轉到InstallUtil.exe的目錄下,我安裝的是VS 2010,對應的目錄為:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe,然后執行InstallUtil.exe 待執行的exe文件的目錄,如:InstallUtil.exe F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe。執行成功后,會在Windows的服務中,出現了剛剛添加的服務的名稱。
6.啟動該服務,這時打開bin\Debug文件夾,發現已經生成了一個test.txt的文件,里面記錄了時間。這說明服務已經正式開始執行。
7.停止服務的操作也和簡單,打開命令行工具,轉到C:\Windows\Microsoft.NET\Framework\v4.0.30319目錄,然后執行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();
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????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個文件信息
- 上一篇:asp.net 上傳進度條
- 下一篇:QQ所有圖標圖片資源含gft文件以及jpg文件
評論
共有 條評論