public void Install() { using (var servicemanager = NativeServiceManager.Open()) { using (var service = servicemanager.CreateService(this.ServiceName, this.ServiceName, "\"" + Assembly.GetExecutingAssembly().Location + "\" -service", ServiceRights.AllAccess)) { service.Start(new string[] { }); } } }
public static NativeServiceManager Open() { NativeServiceManager handle = OpenSCManager(null, null, ServiceManagerRights.Connect | ServiceManagerRights.CreateService); if (handle.IsClosed || handle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } return(handle); }
public void Uninstall() { using (var servicemanager = NativeServiceManager.Open()) { using (var service = servicemanager.OpenService(this.ServiceName, ServiceRights.AllAccess)) { service.Stop(); service.Delete(); } } }
protected static extern NativeService CreateService( NativeServiceManager hSCManager, string lpServiceName, string lpDisplayName, ServiceRights dwDesiredAccess, ServiceType dwServiceType, ServiceStartType dwStartType, ServiceErrorControl dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword );
protected static extern NativeService OpenService( NativeServiceManager hSCManager, string lpServiceName, ServiceRights dwDesiredAccess );