You have created or installed service that uses Start parameters and now want to change them you would expect to be able to change them in Services (services.msc), but unfortunately changes made to „Start parameters“ cannot be applied from that application. But there are few approaches that can help you quickly to fix this issue. In following examples we will be changing Solr 6.6.2 service that was installed by using NSSM (the Non-Sucking Service Manager)
NSSM
If you have just installed your service you can try removing service and adding it again with correct parameters using Command prompt by running following commands (without brackets):
nssm remove {ServiceName}
nssm install {ServiceName} {ServiceLocation} {ServiceParameters}
If removing and adding service is not an option for you there is also an NSSM „set“ command designed for updating your Services by typing:
nssm set solr662new “AppParameters” “start -f -p 8984”
Registry
Another approach would be using Registry. All services from services.msc are being read from one Registry location: „Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\{Service Name}“, and if your service has sub key/folder „Parameters“, expand it and there you can find AppParameters, where you can visually change your „Start parameters“
C#
In following code we are accessing Registry key „Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services“ and looking for service named „solr662new“, after it is found we are updating DisplayName and AppParameters values. If you need to update values other than AppParameters try checking out ServiceRegistryDtoModel and ParametersDtoModel models and UpdateRegistryKey method and update it to suit your needs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace ConsoleApp2 { class Program { private static Type parameterType; private static Type serviceRegistryType; private static List<PropertyInfo> parameterProperties; private static List<PropertyInfo> serviceRegistryTypeParameterProperties; static void Main(string[] args) { parameterType = typeof(ParametersDtoModel); parameterProperties = parameterType.GetProperties().ToList(); serviceRegistryType = typeof(ServiceRegistryDtoModel); serviceRegistryTypeParameterProperties = serviceRegistryType.GetProperties().ToList(); string serviceName = "solr662new"; // Get service with all the keys ServiceRegistryDtoModel result = GetAllServiceKeys(serviceName); // update service ServiceRegistryDtoModel model = new ServiceRegistryDtoModel(); model.DisplayName = "solr662"; model.RegistryKey = @"Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\" + serviceName; model.Parameters.AppParameters = "start -f -p 8985"; model.Parameters.RegistryKey = model.RegistryKey + "\\Parameters"; bool isRegistryKeyUpdated = UpdateRegistryKey(model); } public static ServiceRegistryDtoModel GetAllServiceKeys(string serviceName) { string solrRoot = string.Empty; string serviceArguments = string.Empty; Dictionary<string, object> solrVersions = new Dictionary<string, object>(); RegistryView RegistryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32; ServiceRegistryDtoModel serviceRegistryDto = new ServiceRegistryDtoModel(); using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView)) { using (RegistryKey serviceRootKey = hklm.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + serviceName, false)) { serviceRegistryDto.RegistryKey = serviceRootKey.Name; string[] serviceKeys = serviceRootKey.GetValueNames(); foreach (string serviceKey in serviceKeys) { GetServiceValues(serviceRootKey, serviceRegistryDto, serviceKey); } string parametersSubkey = serviceRootKey.GetSubKeyNames().FirstOrDefault(x => x == "Parameters"); if (parametersSubkey.Equals("Parameters")) { serviceRegistryDto.Parameters = GetParameterSubkeyModelFromKey(serviceName, solrVersions, hklm, parametersSubkey); } } } return serviceRegistryDto; } public static bool UpdateRegistryKey(ServiceRegistryDtoModel model) { RegistryView RegistryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32; using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView)) { // removing "Computer\HKEY_LOCAL_MACHINE" from string // because Registry path should be in format SYSTEM\CurrentControlSet\Services\{ServiceName} string keyOne = model.RegistryKey.Substring(28, model.RegistryKey.Length - 28); using (RegistryKey instanceKey = hklm.OpenSubKey(keyOne, true)) { if (instanceKey != null) { instanceKey.SetValue(nameof(model.DisplayName), model.DisplayName); } else { return false; } } string keyTwo = model.Parameters.RegistryKey.Substring(28, model.Parameters.RegistryKey.Length - 28); using (RegistryKey instanceKey = hklm.OpenSubKey(keyTwo, true)) { if (instanceKey != null) { instanceKey.SetValue(nameof(model.Parameters.AppParameters), model.Parameters.AppParameters); } else { return false; } } } return true; } private static void GetServiceValues(RegistryKey serviceRootKey, ServiceRegistryDtoModel serviceRegistryDto, string serviceKey) { PropertyInfo targetedProperty = serviceRegistryTypeParameterProperties.FirstOrDefault(x => x.Name.Equals(serviceKey)); if (targetedProperty != null) { PropertyInfo propertyInfo = serviceRegistryType.GetProperty(targetedProperty.Name); object value = serviceRootKey.GetValue(serviceKey); propertyInfo.SetValue(serviceRegistryDto, value); } } private static ParametersDtoModel GetParameterSubkeyModelFromKey(string serviceName, Dictionary<string, object> solrVersions, RegistryKey hklm, string subkeyName) { ParametersDtoModel parameterModel = new ParametersDtoModel(); using (RegistryKey parameterKey = hklm.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + serviceName + "\\" + subkeyName, false)) { parameterModel.RegistryKey = parameterKey.Name; foreach (string subkey in parameterKey.GetValueNames()) { parameterModel.RegistryKey = parameterKey.Name; PropertyInfo targetedProperty = parameterProperties.FirstOrDefault(x => x.Name.Equals(subkey)); if (targetedProperty != null) { PropertyInfo piInstance = parameterType.GetProperty(targetedProperty.Name); object value = parameterKey.GetValue(subkey); solrVersions.Add(subkey, value); piInstance.SetValue(parameterModel, value); } } } return parameterModel; } } public class ServiceRegistryDtoModel { public ServiceRegistryDtoModel() { Parameters = new ParametersDtoModel(); } public int DelayedAutostart { get; set; } public string DisplayName { get; set; } public int ErrorControl { get; set; } public int FailureActionsOnNonCrashFailures { get; set; } public string ImagePath { get; set; } public string ObjectName { get; set; } public int Start { get; set; } public int Type { get; set; } public ParametersDtoModel Parameters { get; set; } public string RegistryKey { get; set; } } public class ParametersDtoModel { public string AppDirectory { get; set; } public string Application { get; set; } public string AppParameters { get; set; } public string RegistryKey { get; set; } } } |