How to install a Windows Service without Setup?

I have a Windows service project in Visual Studio in C#, however, I need to install this project from lines of Code, without using the Installutil of the console and neither the setup of Visual Studio.

Do you have any way to do this?

Author: stderr, 2014-04-18

1 answers

One can use the Class ManagedInstallerClass (responsible for dealing with the functionality of Installutil ), more specifically, the method InstallHelper:

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

source

Example of use:

  • install: meuProjeto.exe -install
  • uninstall: meuProjeto.exe -uninstall
 13
Author: stderr, 2017-05-23 12:37:23