C# 以编程方式安装 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2072288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing Windows Service programmatically
提问by Josh
How do I install a Windows Service programmatically without using installutil.exe?
如何在不使用 installutil.exe 的情况下以编程方式安装 Windows 服务?
采纳答案by Mark Redman
You can install the service by adding this code (in the program file, Program.cs) to install itself when run from the commandline using specified parameters:
您可以通过添加此代码(在程序文件 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);
}
}
回答by Matt Davis
I install and uninstall my Windows Service via the command line, e.g., MyWindowsService.exe -install
and MyWindowsService.exe -uninstall
, to avoid using installutil.exe
myself. I've written a set of instructions for how to do this here.
我通过命令行安装和卸载我的 Windows 服务,例如,MyWindowsService.exe -install
和MyWindowsService.exe -uninstall
,以避免使用installutil.exe
我自己。我在此处编写了一组有关如何执行此操作的说明。
回答by Dana Holt
I use the method from the following CodeProject article, and it works great.
我使用以下 CodeProject 文章中的方法,效果很好。
回答by Taki7o7
I cannot comment bc of missing reputation, but regarding Mark Redman's Solution - If you wonder you cannot find your key in the given path, checkout the WOW6432Node
我无法评论缺少声誉的 bc,但关于 Mark Redman 的解决方案 - 如果您想知道在给定的路径中找不到您的密钥,请查看 WOW6432Node
From AdvancedInstaller:
从AdvancedInstaller:
"The Wow6432Node
registry entry indicates that you are running a 64-bit Windows version.
“Wow6432Node
注册表项表明您运行的是 64 位 Windows 版本。
The operating system uses this key to display a separate view of HKEY_LOCAL_MACHINE\SOFTWARE for 32-bit applications that run on 64-bit Windows versions. When a 32-bit application writes or reads a value under the HKEY_LOCAL_MACHINE\SOFTWARE\<company>\<product>
subkey, the application reads from the HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\<company>\<product> subkey.
操作系统使用此键为在 64 位 Windows 版本上运行的 32 位应用程序显示 HKEY_LOCAL_MACHINE\SOFTWARE 的单独视图。当 32 位应用程序写入或读取HKEY_LOCAL_MACHINE\SOFTWARE\<company>\<product>
子项下的值时,应用程序从HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\<company>\<product> subkey.
A registry reflector copies certain values between the 32-bit and 64-bit registry views (mainly for COM registration) and resolves any conflicts using a "last-writer-wins" approach."
注册表反射器在 32 位和 64 位注册表视图之间复制某些值(主要用于 COM 注册),并使用“最后写入者获胜”的方法解决任何冲突。