如何在不使用installutil.exe的情况下以编程方式安装Windows服务?
如何在不使用installutil.exe的情况下以编程方式安装Windows服务?
您可以通过将以下代码(在程序文件Program.cs中)添加到命令行中使用指定参数运行时安装自身来安装服务:
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
if (System.Environment.UserInteractive)
{
if (args.Length > 0)
{
switch (args[0])
{
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 MyService() };
ServiceBase.Run(ServicesToRun);
}
}
ManagedInstallerClass.InstallHelper(srvExe)
,这个应用程序会等待 InstallHelper
退出(安装完成)吗?谢谢。 - Andrey K.我使用了下面这篇CodeProject文章中提到的方法,它非常棒。
ManagedInstallerClass.InstallHelper(srvExe)
,这个应用程序会等待 InstallHelper
退出(安装完成)吗?谢谢。 - Andrey K.我通过命令行安装和卸载Windows服务,例如MyWindowsService.exe -install
和MyWindowsService.exe -uninstall
,以避免使用installutil.exe
。 我已经编写了一组说明,说明如何在这里执行此操作。
由于声誉不足,我无法发表评论,但关于马克·雷德曼的解决方案 - 如果你想知道在给定路径中找不到你的键,可以看看 WOW6432Node
“Wow6432Node
注册表条目表示您正在运行的是64位Windows版本。
操作系统使用此键来为在64位Windows版本上运行的32位应用程序显示HKEY_LOCAL_MACHINE\SOFTWARE的单独视图。当32位应用程序在 HKEY_LOCAL_MACHINE\SOFTWARE\<company>\<product>
子键下写入或读取值时,该应用程序从 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\<company>\<product>
子键中读取。
注册表反射器在32位和64位注册表视图之间复制某些值(主要用于COM注册),并使用“最后写入者胜出”的方法解决任何冲突。”
sc.exe
,可能会有解决方案。