Batch Script to Install or Uninstall a .NET Windows Service

后端 未结 10 1555
我在风中等你
我在风中等你 2021-01-31 05:13

I have no experience writing batch scripts, but I was wondering if there was a way to install a .NET Windows service using installutil.exe using such a script, or u

10条回答
  •  离开以前
    2021-01-31 05:58

    It is easier to just make self-installing services. Once you implement this, you can either run the service exe directly with the (/i or /u switch), or wrap the call in a batch file if you'd like.

    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            //Install service
            if (args[0].Trim().ToLower() == "/i")
            { System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "/i", Assembly.GetExecutingAssembly().Location }); }
    
            //Uninstall service                 
            else if (args[0].Trim().ToLower() == "/u")
            { System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); }
        }
        else
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
    }
    

提交回复
热议问题