Installing Windows Service programmatically

后端 未结 4 1623
Happy的楠姐
Happy的楠姐 2020-12-02 06:43

How do I install a Windows Service programmatically without using installutil.exe?

相关标签:
4条回答
  • 2020-12-02 06:59

    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

    From AdvancedInstaller:

    "The Wow6432Node registry entry indicates that you are running a 64-bit Windows version.

    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.

    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."

    0 讨论(0)
  • 2020-12-02 07:04

    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.

    0 讨论(0)
  • 2020-12-02 07:05

    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:

    /// <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);
                }
            }
    
    0 讨论(0)
  • 2020-12-02 07:12

    I use the method from the following CodeProject article, and it works great.

    Windows Services Can Install Themselves

    0 讨论(0)
提交回复
热议问题