c# - 以编程方式安装 Windows 服务

标签 c# windows-services

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

关于c# - 以编程方式安装 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2072288/

相关文章:

c# - 在 ado.net 中显示有关数据库的信息

c# - Unity 2D 中的运动卡顿

c# - 在长时间运行的 Windows 服务中使用计时器

c# - Topshelf 中托管的 WCF 服务需要很长时间才能关闭

.net - 如何设置 StructureMap 来解决 Windows 服务中的依赖关系?

c# - 如何正确命名域 namespace 内的类?

c# - 确定将连接池的最大池大小提高到什么程度

windows - SCardEstablishContext 作为服务挂起

c# - 授予远程用户(非管理员)使用 WMI 和 C# 枚举命名空间 cimv2 中 Win32_Service 中的服务的能力

c# - 通过 ANTLR 和 DLR 扩展 C#