Visual C# 2010 Recipies有一个示例,它将向您准确展示如何执行此操作,我已尝试使用 VS 2008 和 .NET 3.5。
相当于:
将 Service Installer 类添加到您的 Windows 服务项目 - 它将类似于下面的代码片段:
[RunInstaller(true)]
public partial class PollingServiceInstaller : Installer
{
public PollingServiceInstaller()
{
//Instantiate and configure a ServiceProcessInstaller
ServiceProcessInstaller PollingService = new ServiceProcessInstaller();
PollingService.Account = ServiceAccount.LocalSystem;
//Instantiate and configure a ServiceInstaller
ServiceInstaller PollingInstaller = new ServiceInstaller();
PollingInstaller.DisplayName = "SMMD Polling Service Beta";
PollingInstaller.ServiceName = "SMMD Polling Service Beta";
PollingInstaller.StartType = ServiceStartMode.Automatic;
//Add both the service process installer and the service installer to the
//Installers collection, which is inherited from the Installer base class.
Installers.Add(PollingInstaller);
Installers.Add(PollingService);
}
}
Run Code Online (Sandbox Code Playgroud)最后,您将使用命令行实用程序来实际安装该服务 - 您可以在此处了解其工作原理:
如果您有任何疑问,请告诉我。