要使服務能進行安裝,首先要使整個服務的程序集存在一個Installer類
下面貼出代碼:
//使該類可被安裝程序調用安裝
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
public ProjectInstaller()
{
this.spInstaller = new ServiceProcessInstaller();
this.sInstaller = new ServiceInstaller();
//帳戶類型
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Username = null;
this.spInstaller.Password = null;
//服務名
this.sInstaller.ServiceName = "您的服務名";
//服務啟動方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });
}
protected override void OnAfterInstall(System.Collections.IDictionary savedState) {
base.OnAfterInstall(savedState);
}
}
在系統中安裝服務(鍵盤Win+R):
%SystemRoot%Microsoft.NETFrameworkv2.0.50727installutil XXX.exe(程序集路徑)
在系統中卸載服務(鍵盤Win+R):
%SystemRoot%Microsoft.NETFrameworkv2.0.50727installutil /u XXX.exe(程序集路徑)