public static ReturnValue InstallService(string svcName, string svcDispName, string svcPath, ServiceType svcType, OnError errHandle, StartMode svcStartMode, bool interactWithDesktop, string svcStartName, string svcPassword, string loadOrderGroup, string[] loadOrderGroupDependencies, string[] svcDependencies) { ManagementClass mc = new ManagementClass("Win32_Service"); ManagementBaseObject inParams = mc.GetMethodParameters("create"); inParams["Name"] = svcName; inParams["DisplayName"] = svcDispName; inParams["PathName"] = svcPath; inParams["ServiceType"] = svcType; inParams["ErrorControl"] = errHandle; inParams["StartMode"] = svcStartMode.ToString(); inParams["DesktopInteract"] = interactWithDesktop; inParams["StartName"] = svcStartName; inParams["StartPassword"] = svcPassword; inParams["LoadOrderGroup"] = loadOrderGroup; inParams["LoadOrderGroupDependencies"] = loadOrderGroupDependencies; inParams["ServiceDependencies"] = svcDependencies; try { ManagementBaseObject outParams = mc.InvokeMethod("create", inParams, null); return((ReturnValue)Enum.Parse(typeof(ReturnValue), outParams["ReturnValue"].ToString())); } catch (Exception ex) { throw ex; } }
void StartBehaviour([CallerMemberName] string caller = "") { if (caller != startAt.ToString()) { return; } Resume(); }
public static string GetStartModeString(StartMode startMode) { switch (startMode) { case StartMode.Auto: return("Automatic"); } return(startMode.ToString()); }
public static ReturnValue ChangeStartMode(string svcName, StartMode startMode) { string objPath = string.Format("Win32_Service.Name='{0}'", svcName); using (ManagementObject service = new ManagementObject(new ManagementPath(objPath))) { ManagementBaseObject inParams = service.GetMethodParameters("ChangeStartMode"); inParams["StartMode"] = startMode.ToString(); try { ManagementBaseObject outParams = service.InvokeMethod("ChangeStartMode", inParams, null); return((ReturnValue)Enum.Parse(typeof(ReturnValue), outParams["ReturnValue"].ToString())); } catch (Exception ex) { throw ex; } } }
/// <summary> /// Changes the start mode. /// </summary> /// <param name="svcName">Name of the SVC.</param> /// <param name="startMode">The start mode.</param> /// <returns>ReturnValue.</returns> public ReturnValue ChangeStartMode(string svcName, StartMode startMode) { string objPath = string.Format("Win32_Service.Name='{0}'", svcName); using (ManagementObject service = new ManagementObject(ManScope, new ManagementPath(objPath), new ObjectGetOptions())) { ManagementBaseObject inParams = service.GetMethodParameters("ChangeStartMode"); inParams["StartMode"] = startMode.ToString(); try { ManagementBaseObject outParams = service.InvokeMethod("ChangeStartMode", inParams, null); return((ReturnValue)Enum.Parse(typeof(ReturnValue), outParams["ReturnValue"].ToString())); } catch (Exception ex) { CDFMonitor.LogOutputHandler("ChangeStartMode:exception:" + ex.ToString()); return(ReturnValue.UnknownFailure); } } }
public static ReturnValue InstallService(string svcName, string svcDispName, string svcPath, ServiceType svcType, OnError errHandle, StartMode svcStartMode, bool interactWithDesktop, string svcStartName, string svcPassword, string loadOrderGroup, string[] loadOrderGroupDependencies, string[] svcDependencies) { var mc = new ManagementClass("Win32_Service"); ManagementBaseObject inParams = mc.GetMethodParameters("create"); inParams["Name"] = svcName; inParams["DisplayName"] = svcDispName; inParams["PathName"] = svcPath; inParams["ServiceType"] = svcType; inParams["ErrorControl"] = errHandle; inParams["StartMode"] = svcStartMode.ToString(); inParams["DesktopInteract"] = interactWithDesktop; inParams["StartName"] = svcStartName; inParams["StartPassword"] = svcPassword; inParams["LoadOrderGroup"] = loadOrderGroup; inParams["LoadOrderGroupDependencies"] = loadOrderGroupDependencies; inParams["ServiceDependencies"] = svcDependencies; try { ManagementBaseObject outParams = mc.InvokeMethod("create", inParams, null); return (ReturnValue) Enum.Parse(typeof (ReturnValue), outParams["ReturnValue"].ToString()); } catch (Exception ex) { throw ex; } }
public static ReturnValue ChangeStartMode(string svcName, StartMode startMode) { string objPath = string.Format("Win32_Service.Name='{0}'", svcName); using (var service = new ManagementObject(new ManagementPath(objPath))) { ManagementBaseObject inParams = service.GetMethodParameters("ChangeStartMode"); inParams["StartMode"] = startMode.ToString(); try { ManagementBaseObject outParams = service.InvokeMethod("ChangeStartMode", inParams, null); return (ReturnValue) Enum.Parse(typeof (ReturnValue), outParams["ReturnValue"].ToString()); } catch (Exception ex) { throw ex; } } }
/// <summary> /// Installs the service. /// </summary> /// <param name="svcName">Name of the SVC.</param> /// <param name="svcDispName">Name of the SVC disp.</param> /// <param name="svcPath">The SVC path.</param> /// <param name="svcType">Type of the SVC.</param> /// <param name="errHandle">The err handle.</param> /// <param name="svcStartMode">The SVC start mode.</param> /// <param name="interactWithDesktop">if set to <c>true</c> [interact with desktop].</param> /// <param name="creds">The creds.</param> /// <param name="loadOrderGroup">The load order group.</param> /// <param name="loadOrderGroupDependencies">The load order group dependencies.</param> /// <param name="svcDependencies">The SVC dependencies.</param> /// <returns>ReturnValue.</returns> public ReturnValue InstallService(string svcName, string svcDispName, string svcPath, ServiceType svcType, OnError errHandle, StartMode svcStartMode, bool interactWithDesktop, NetworkCredential creds, string loadOrderGroup, string[] loadOrderGroupDependencies, string[] svcDependencies) { CDFMonitor.LogOutputHandler("InstallService:enter"); ReturnValue retval = ReturnValue.UnknownFailure; string svcPassword = creds.Password.ToString(); string svcStartName = creds.UserName.ToString(); if (!CDFMonitor.Instance.Config.AppSettings.UseServiceCredentials) { svcStartName = string.Empty; svcPassword = string.Empty; } // fixes issue when using local credentials stored in utility creds else if (creds.Domain != Properties.Resources.SessionName) { svcStartName = string.Format("{0}\\{1}", string.IsNullOrEmpty(creds.Domain) ? "." : creds.Domain, creds.UserName); } else if (creds.Domain == Properties.Resources.SessionName) { svcStartName = string.Empty; svcPassword = string.Empty; } if (string.IsNullOrEmpty(svcStartName) || string.IsNullOrEmpty(svcPassword)) { svcStartName = "LocalSystem"; svcPassword = string.Empty; } CDFMonitor.LogOutputHandler("InstallService:user:"******"Win32_Service"), new ObjectGetOptions()); ManagementBaseObject inParams = mc.GetMethodParameters("create"); inParams["Name"] = svcName; inParams["DisplayName"] = svcDispName; inParams["PathName"] = svcPath; inParams["ServiceType"] = svcType; inParams["ErrorControl"] = errHandle; inParams["StartMode"] = svcStartMode.ToString(); inParams["DesktopInteract"] = interactWithDesktop; inParams["StartName"] = svcStartName; // ".\\" + svcStartName; inParams["StartPassword"] = svcPassword; inParams["LoadOrderGroup"] = loadOrderGroup; inParams["LoadOrderGroupDependencies"] = loadOrderGroupDependencies; inParams["ServiceDependencies"] = svcDependencies; try { ManagementBaseObject outParams = mc.InvokeMethod("create", inParams, null); retval = (ReturnValue)Enum.Parse(typeof(ReturnValue), outParams["ReturnValue"].ToString()); CDFMonitor.LogOutputHandler("InstallService:exit:return:" + retval); return(retval); } catch (Exception ex) { CDFMonitor.LogOutputHandler("InstallService:exception:" + ex.ToString()); return(retval); } }
public static string GetStartModeString(StartMode startMode) { switch(startMode) { case StartMode.Auto: return "Automatic"; } return startMode.ToString(); }