使用Vs的 Windows Service模板可以很方便地开发Windows 服务。在Service文件 的设计界面,右键点击Add Installer,可以添加一个ProjectInstaller类, 然后设定服务的运行账号,名称等。
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.Description = "Sample Service Description";
this.serviceInstaller1.DisplayName = "Sample Service";
this.serviceInstaller1.ServiceName = "SampleService";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
#endregion
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;开发完毕后,可以使用.Net Framework自带的InstallUtil.exe安装服务。
installutil [/u[ninstall]] [options] assembly [[options] assembly] …
如果想在安装的时候指定服务名,或者想在同一台机器上安装多份服务,可以采用下面的方式:
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
然后使用下面的命令安装:
sc create <servicename> binpath="<pathtobinaryexecutable>" [option1] [option2] [optionN]

本文介绍如何使用Visual Studio的Windows Service模板快速开发Windows服务,并通过添加ProjectInstaller类来配置服务属性,如运行账号和服务名称等。文章还展示了如何利用.Net Framework的InstallUtil.exe工具安装服务,以及如何在安装过程中指定服务名。

5710

被折叠的 条评论
为什么被折叠?



