Last active
May 3, 2024 05:55
-
-
Save joshgo/4627280 to your computer and use it in GitHub Desktop.
self install windows service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.ServiceProcess; | |
| using System.Text; | |
| using System.Reflection; | |
| using System.Configuration.Install; | |
| namespace test | |
| { | |
| static class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| if (Environment.UserInteractive) | |
| { | |
| // If placed in a shared DLL, change to .GetCallingAssembly() | |
| var assembly = Assembly.GetExecutingAssembly(); | |
| string parameter = args.Length > 0 ? args[0] : ""; | |
| switch (parameter) | |
| { | |
| case "--install": | |
| case "-i": | |
| case "/i": | |
| ManagedInstallerClass.InstallHelper(new string[] { assembly.Location }); | |
| break; | |
| case "--uninstall": | |
| case "-u": | |
| case "/u": | |
| ManagedInstallerClass.InstallHelper(new string[] { "/u", assembly.Location }); | |
| break; | |
| default: | |
| using (var service = new TestService()) | |
| { | |
| service.StartDebug(); | |
| Console.WriteLine("Press [ENTER] ..."); | |
| Console.ReadLine(); | |
| } | |
| break; | |
| } | |
| } | |
| else | |
| { | |
| ServiceBase.Run(new TestService()); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment