Created
March 15, 2013 12:35
-
-
Save stephen-james/5169593 to your computer and use it in GitHub Desktop.
Service that can run in command line
This file contains 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
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
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 | |
{ | |
// just run the action as a once off if this was invoked User Interactive | |
// (ServiceAction is just a class that implements whatever the service should do to abstract the | |
// actual action from the guts of the service lifecycle. ie the service plumbing code) | |
(new ServiceAction()).PerformAction(); | |
} | |
} | |
else | |
{ | |
var servicesToRun = new ServiceBase[] { new WhateverService() }; | |
ServiceBase.Run(servicesToRun); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment