我正在尝试在不使用 installutil 的情况下安装 Windows 服务。我发现一种可以理解且直接的方法是使用:
ManagedInstallerClass.InstallHelper
所以我最终得到了以下 Program.cs:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
if (args.Length >0)
{
string parameter = string.Concat(args);
switch (parameter)
{
case "--install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "--uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new PicknikService()
};
ServiceBase.Run(ServicesToRun);
}
}
}
在我构建服务并执行 MyService.exe --install 之后,我得到以下信息:
Cannot start service from the command line or debugger. A winwows Service must first be installed(using installutil.exe) and then started with the ServerExplorer, Windows Services Afministrative tool or the NET START command.
有什么想法吗?