среда, 3 декабря 2008 г.

How to install (uninstall) Windows service programmatically

First, You should add reference to System.Configuration.Install

Install service:

            string ServiceFileName = @"c:\myservice.exe";
            System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller();
            Installer.Path = ServiceFileName;
            Installer.UseNewContext = true;
            Installer.Install(null);
            Installer.Commit(null);

Uninstall service:

            string ServiceFileName = @"c:\myservice.exe";
            System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller();
            Installer.Path = ServiceFileName;
            Installer.UseNewContext = true;
            Installer.Uninstall(null);