Skip to content

Instantly share code, notes, and snippets.

@benpturner
Last active November 29, 2023 10:58
Show Gist options
  • Save benpturner/e5e632ea092643bf9c075c9b2a7de6f6 to your computer and use it in GitHub Desktop.
Save benpturner/e5e632ea092643bf9c075c9b2a7de6f6 to your computer and use it in GitHub Desktop.
Simple C# Service
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