Last active
November 29, 2023 10:58
-
-
Save benpturner/e5e632ea092643bf9c075c9b2a7de6f6 to your computer and use it in GitHub Desktop.
Simple C# Service
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
using System.ServiceProcess; | |
using System.Diagnostics; | |
public class MyService : ServiceBase | |
{ | |
public MyService() | |
{ | |
ServiceName = "runner"; | |
} | |
protected override void OnStart(string[] args) | |
{ | |
// Code to run when the service starts | |
string binaryPath = "C:\\programdata\\run.exe"; | |
ProcessStartInfo startInfo = new ProcessStartInfo(binaryPath); | |
startInfo.CreateNoWindow = true; | |
startInfo.UseShellExecute = false; | |
Process process = new Process(); | |
process.StartInfo = startInfo; | |
process.Start(); | |
} | |
protected override void OnStop() | |
{ | |
// Code to run when the service stops | |
} | |
public static void Main() | |
{ | |
ServiceBase[] ServicesToRun; | |
ServicesToRun = new ServiceBase[] | |
{ | |
new MyService() | |
}; | |
ServiceBase.Run(ServicesToRun); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment