public ActionResult Services(string serviceNameParam, int?id) { ServiceViewModel service = new ServiceViewModel(serviceNameParam); var credentials = new UserCredentials(service.Username, service.Password); Impersonation.RunAsUser(credentials, SimpleImpersonation.LogonType.Interactive, () => { ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, service.Server, service.Name); scp.Assert(); ServiceController sc = new ServiceController(service.Name, service.Server); TimeSpan timeout = new TimeSpan(0, 0, 30); switch (id) { case 0: sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped, timeout); break; case 1: sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running, timeout); break; default: service.Status = sc.Status.ToString(); break; } }); return(View(service)); }
private void RunCmd(int cmd) { ServiceController sc = new ServiceController("LibCECService", Environment.MachineName); if (sc.Status != ServiceControllerStatus.Running) { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMinutes(1)); } try { ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, "LibCECService");//this will grant permission to access the Service scp.Assert(); sc.Refresh(); sc.ExecuteCommand(cmd); } catch (Exception e) { MessageBox.Show(e.Message, "LibCEC Service Installer", MessageBoxButton.OK, MessageBoxImage.Information); } }
private void ControlForm1_Shown(object sender, EventArgs e) { if (scp == null) { scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, "Service1");//this will grant permission to access the Service } //slc.ClientConnect(); //service_log_poll_worker.RunWorkerAsync(); scp.Assert(); serviceController1.Refresh(); label1.Text = serviceController1.DisplayName + " 서비스 "; }
public static void CustomCommend(string serviceName, int commend) { try { ServiceController sc = new ServiceController(serviceName); ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, serviceName);//this will grant permission to access the Service scp.Assert(); sc.Refresh(); sc.ExecuteCommand(commend); } catch { } }
// 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(); }