簡體   English   中英

以編程方式安裝 Windows 服務

[英]Installing Windows Service programmatically

如何在不使用 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);
            }
        }

我使用以下 CodeProject 文章中的方法,效果很好。

Windows 服務可以自行安裝

我通過命令行安裝和卸載我的 Windows 服務,例如MyWindowsService.exe -installMyWindowsService.exe -uninstall ,以避免自己使用installutil.exe 我在此處編寫了一組有關如何執行此操作的說明。

我無法評論缺少聲譽的 bc,但關於 Mark Redman 的解決方案 - 如果您想知道在給定路徑中找不到您的密鑰,請WOW6432Node

AdvancedInstaller

Wow6432Node注冊表項表明您運行的是 64 位 Windows 版本。

操作系統使用此鍵為在 64 位 Windows 版本上運行的 32 位應用程序顯示 HKEY_LOCAL_MACHINE\\SOFTWARE 的單獨視圖。 當 32 位應用程序在HKEY_LOCAL_MACHINE\\SOFTWARE\\<company>\\<product>子項下寫入或讀取值時,應用程序從HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\<company>\\<product> subkey.讀取HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\<company>\\<product> subkey.

注冊表反射器在 32 位和 64 位注冊表視圖之間復制某些值(主要用於 COM 注冊),並使用“最后寫入者獲勝”的方法解決任何沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM