简体   繁体   English

以编程方式安装 Windows 服务

[英]Installing Windows Service programmatically

如何在不使用 installutil.exe 的情况下以编程方式安装 Windows 服务?

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);
            }
        }

I use the method from the following CodeProject article, and it works great.我使用以下 CodeProject 文章中的方法,效果很好。

Windows Services Can Install Themselves Windows 服务可以自行安装

I install and uninstall my Windows Service via the command line, eg, MyWindowsService.exe -install and MyWindowsService.exe -uninstall , to avoid using installutil.exe myself.我通过命令行安装和卸载我的 Windows 服务,例如MyWindowsService.exe -installMyWindowsService.exe -uninstall ,以避免自己使用installutil.exe I've written a set of instructions for how to do this here .我在此处编写了一组有关如何执行此操作的说明。

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.操作系统使用此键为在 64 位 Windows 版本上运行的 32 位应用程序显示 HKEY_LOCAL_MACHINE\\SOFTWARE 的单独视图。 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.当 32 位应用程序在HKEY_LOCAL_MACHINE\\SOFTWARE\\<company>\\<product>子项下写入或读取值时,应用程序从HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\<company>\\<product> subkey.读取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 注册),并使用“最后写入者获胜”的方法解决任何冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM