How to install a Windows Service without Setup?

12

I have a Windows Service project in Visual Studio in C #, however, I need to install this project from lines of code, without using #

  • Is there any way to do this?

        
    asked by anonymous 18.04.2014 / 15:28

    1 answer

    13

    You can use the ManagedInstallerClass (responsible for dealing with the 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);
        }
    }
    

    Font

    Example usage:

    • Install: meuProjeto.exe -install
    • Uninstall: meuProjeto.exe -uninstall
    18.04.2014 / 15:44