.NET self-registering Windows Service
using System.Configuration.Install;
using System.Linq;
using System.Windows.Forms;
namespace MyService
{
class Program
{
static void Main(string[] args)
{
if (args.Any(item => item == "/install"))
{
ServiceSelfInstaller.Install();
}
else if
(args.Any(item => item == "/uninstall"))
ServiceSelfInstaller.Uninstall();
else
{
//Run Service...
}
}
internal static class ServiceSelfInstaller
{
public static void Install()
{
ManagedInstallerClass.InstallHelper(new[] { Application.ExecutablePath });
}
public static void Uninstall()
{
ManagedInstallerClass.InstallHelper(new[] { "/u", Application.ExecutablePath });
}
}
}
}