internal static void RegisterNewService(WindowsServiceDetails serviceInfo, params string[] serviceDependencies)
        {
            using (var win32Service = new ManagementClass("Win32_Service"))
                using (var inParams = win32Service.GetMethodParameters("Create"))
                {
                    inParams["Name"]                       = serviceInfo.Name;
                    inParams["DisplayName"]                = serviceInfo.DisplayName;
                    inParams["PathName"]                   = serviceInfo.ImagePath;
                    inParams["ServiceType"]                = 16; // Own Process
                    inParams["ErrorControl"]               = 1;  //Report to user
                    inParams["StartMode"]                  = "Automatic";
                    inParams["DesktopInteract"]            = false;
                    inParams["StartName"]                  = ConvertAccountNameToServiceAccount(serviceInfo.ServiceAccount);
                    inParams["StartPassword"]              = serviceInfo.ServiceAccountPwd;
                    inParams["LoadOrderGroup"]             = null;
                    inParams["LoadOrderGroupDependencies"] = null;
                    inParams["ServiceDependencies"]        = serviceDependencies;

                    var outParams = win32Service.InvokeMethod("create", inParams, null);
                    if (outParams == null)
                    {
                        throw new ManagementException($"Failed to create service {serviceInfo.Name}");
                    }

                    var wmiReturnCode = Convert.ToInt32(outParams["ReturnValue"]);
                    if (wmiReturnCode != 0)
                    {
                        var message = wmiReturnCode < Win32ChangeErrorMessages.Length
                        ? $"Failed to create service to {serviceInfo.Name} - {Win32ServiceErrorMessages[wmiReturnCode]}"
                        : "An unknown error occurred";

                        if (wmiReturnCode == 22)
                        {
                            message += $"( AccountName {serviceInfo.ServiceAccount} converted to {ConvertAccountNameToServiceAccount(serviceInfo.ServiceAccount)})";
                        }


                        throw new ManagementException(message);
                    }

                    if (!string.IsNullOrWhiteSpace(serviceInfo.ServiceDescription))
                    {
                        using (var servicesBaseKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Services"))
                        {
                            if (servicesBaseKey != null)
                            {
                                using (var serviceKey = servicesBaseKey.OpenSubKey(serviceInfo.Name, true))
                                {
                                    serviceKey?.SetValue("Description", serviceInfo.ServiceDescription);
                                }
                            }
                        }
                    }

                    ServiceRecoveryHelper.SetRecoveryOptions(serviceInfo.Name);
                }
        }
 public static void SetRecoveryOptions(string serviceName)
 {
     try
     {
         using (var helper = new ServiceRecoveryHelper())
         {
             helper.SetRestartOnFailure(serviceName);
         }
     }
     catch (Exception ex)
     {
         Debug.Write(ex);
     }
 }