private void Button_Click(object sender, RoutedEventArgs e) { string svcPath = Path.Combine(Environment.CurrentDirectory, "DS4ToolService.exe"); ServiceController sc = new ServiceController(Constants.SERVICE_NAME, Environment.MachineName); ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, Constants.SERVICE_NAME); scp.Assert(); sc.Refresh(); ServiceInstaller si = new ServiceInstaller(); if (si.DoesServiceExist(Constants.SERVICE_NAME)) { if (sc.Status == ServiceControllerStatus.Running) sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped); si.UnInstallService(Constants.SERVICE_NAME); MessageBox.Show("Service removed"); } EventLog eventLog = new EventLog(); eventLog.Source = Constants.SERVICE_NAME; eventLog.Log = "Application"; if (!EventLog.SourceExists(eventLog.Source)) { EventLog.DeleteEventSource(eventLog.Source, Environment.MachineName); MessageBox.Show("EventLog removed"); } }
// Service start, if the service isn't active, we run it. Otherwise, nothing is done. /// <summary> /// Starting Service /// </summary> private static void StartService(){ ImpersonateUser iu = new ImpersonateUser(); iu.Impersonate(@".", "Axel", Settings.Default.Password); using (var controller = new ServiceController("RedXService")){ ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, ".", "RedXService"); scp.Assert(); controller.Refresh(); if (controller.Status != ServiceControllerStatus.Running){ controller.Start(); } controller.WaitForStatus(ServiceControllerStatus.StartPending); controller.WaitForStatus(ServiceControllerStatus.Running); controller.Refresh(); } iu.Undo(); }