public static bool IsDaemonMasterService(string serviceName)
        {
            try
            {
                //For new system
                using (RegistryKey keyNew = Registry.LocalMachine.OpenSubKey(ServiceRegPath + serviceName, false))
                {
                    if (keyNew != null)
                    {
                        //Get the exe path of the service to determine later if its a service from DaemonMaster
                        string serviceExePath = Convert.ToString(keyNew.GetValue("ImagePath") ?? string.Empty);

                        //Check the path
                        if (!string.IsNullOrWhiteSpace(serviceExePath) && DaemonMasterUtils.ComparePaths(serviceExePath, ServiceControlManager.DmServiceExe))
                        {
                            return(true); //New system
                        }
                    }
                }

                return(false);
            }
            catch
            {
                return(false);
            }
        }
        public static List <DmServiceDefinition> GetInstalledServices()
        {
            var services = new List <DmServiceDefinition>();

            using RegistryKey mainKey = Registry.LocalMachine.OpenSubKey(ServiceRegPath, RegistryKeyPermissionCheck.ReadSubTree);
            if (mainKey == null)
            {
                return(services);
            }

            foreach (string serviceName in mainKey.GetSubKeyNames())
            {
                using (RegistryKey key = mainKey.OpenSubKey(serviceName, RegistryKeyPermissionCheck.ReadSubTree))
                {
                    //If the key invalid, skip this service
                    if (key == null)
                    {
                        continue;
                    }

                    //Get the exe path of the service to determine later if its a service from DaemonMaster
                    string serviceExePath = Convert.ToString(key.GetValue("ImagePath"));

                    //Check service path
                    if (string.IsNullOrWhiteSpace(serviceExePath) || !DaemonMasterUtils.ComparePaths(serviceExePath, ServiceControlManager.DmServiceExe))
                    {
                        continue;
                    }

                    var serviceDefinition = new DmServiceDefinition(serviceName)
                    {
                        DisplayName = Convert.ToString(key.GetValue("DisplayName")),
                        Credentials = new ServiceCredentials(Convert.ToString(key.GetValue("ObjectName", ServiceCredentials.LocalSystem)), null),
                    };

                    using (RegistryKey parameters = key.OpenSubKey("Parameters", RegistryKeyPermissionCheck.ReadSubTree))
                    {
                        //If the key invalid, skip it (this key is not important for the service)
                        if (parameters != null)
                        {
                            serviceDefinition.BinaryPath = Convert.ToString(parameters.GetValue("BinaryPath"));
                        }
                    }

                    services.Add(serviceDefinition);
                }
            }

            return(services);
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        //                                             METHODS                                                  //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static void SaveInRegistry(DmServiceDefinition serviceDefinition)
        {
            using (RegistryKey key = Registry.LocalMachine.CreateSubKey(ServiceRegPath + serviceDefinition.ServiceName, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                //Open Parameters SubKey
                using (RegistryKey parameters = key.CreateSubKey("Parameters", RegistryKeyPermissionCheck.ReadWriteSubTree))
                {
                    //Strings
                    parameters.SetValue("BinaryPath", serviceDefinition.BinaryPath, RegistryValueKind.String);
                    parameters.SetValue("Arguments", serviceDefinition.Arguments, RegistryValueKind.String);

                    //Ints
                    parameters.SetValue("ProcessMaxRestarts", serviceDefinition.ProcessMaxRestarts, RegistryValueKind.DWord);
                    parameters.SetValue("ProcessTimeoutTime", serviceDefinition.ProcessTimeoutTime, RegistryValueKind.DWord);
                    parameters.SetValue("ProcessRestartDelay", serviceDefinition.ProcessRestartDelay, RegistryValueKind.DWord);
                    parameters.SetValue("CounterResetTime", serviceDefinition.CounterResetTime, RegistryValueKind.DWord);
                    parameters.SetValue("ProcessPriority", serviceDefinition.ProcessPriority, RegistryValueKind.DWord);

                    //Bools
                    parameters.SetValue("IsConsoleApplication", serviceDefinition.IsConsoleApplication, RegistryValueKind.DWord);
                    parameters.SetValue("UseCtrlC", serviceDefinition.UseCtrlC, RegistryValueKind.DWord);
                    parameters.SetValue("CanInteractWithDesktop", serviceDefinition.CanInteractWithDesktop, RegistryValueKind.DWord);
                    parameters.SetValue("UseEventLog", serviceDefinition.UseEventLog, RegistryValueKind.DWord);
                }


                //Create an give the user the permission to write to this key (needed for save the PID of the process if it's not the LocalSystem account)
                using (RegistryKey processInfo = key.CreateSubKey("ProcessInfo", RegistryKeyPermissionCheck.ReadWriteSubTree))
                {
                    #region Setting permissions
                    //Only needed when user account has changed
                    if (processInfo != null && !Equals(serviceDefinition.Credentials, ServiceCredentials.NoChange))
                    {
                        //Create a new RegistrySecurity object
                        var rs = new RegistrySecurity();

                        //  Author: Nick Sarabyn - https://stackoverflow.com/questions/3282656/setting-inheritance-and-propagation-flags-with-set-acl-and-powershell
                        //  ╔═════════════╦═════════════╦═════════════════════════════════╦══════════════════════════╦══════════════════╦═════════════════════════╦═══════════════╦═════════════╗
                        //  ║             ║ folder only ║ folder, sub - folders and files ║ folder and sub - folders ║ folder and files ║ sub - folders and files ║ sub - folders ║    files    ║
                        //  ╠═════════════╬═════════════╬═════════════════════════════════╬══════════════════════════╬══════════════════╬═════════════════════════╬═══════════════╬═════════════╣
                        //  ║ Propagation ║ none        ║ none                            ║ none                     ║ none             ║ InheritOnly             ║ InheritOnly   ║ InheritOnly ║
                        //  ║ Inheritance ║ none        ║ Container|Object                ║ Container                ║ Object           ║ Container|Object        ║ Container     ║ Object      ║
                        //  ╚═════════════╩═════════════╩═════════════════════════════════╩══════════════════════════╩══════════════════╩═════════════════════════╩═══════════════╩═════════════╝

                        ////Add access rule for user (only when it is not LocalSystem)
                        if (Equals(serviceDefinition.Credentials, ServiceCredentials.LocalSystem))
                        {
                            rs.AddAccessRule(new RegistryAccessRule((NTAccount) new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null).Translate(typeof(NTAccount)), RegistryRights.WriteKey, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
                        }
                        else if (Equals(serviceDefinition.Credentials, ServiceCredentials.LocalService))
                        {
                            rs.AddAccessRule(new RegistryAccessRule((NTAccount) new SecurityIdentifier(WellKnownSidType.LocalServiceSid, null).Translate(typeof(NTAccount)), RegistryRights.WriteKey, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
                        }
                        else if (Equals(serviceDefinition.Credentials, ServiceCredentials.NetworkService))
                        {
                            rs.AddAccessRule(new RegistryAccessRule((NTAccount) new SecurityIdentifier(WellKnownSidType.NetworkServiceSid, null).Translate(typeof(NTAccount)), RegistryRights.WriteKey, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
                        }
                        else
                        {
                            rs.AddAccessRule(new RegistryAccessRule(new NTAccount(DaemonMasterUtils.GetDomainFromUsername(serviceDefinition.Credentials.Username), DaemonMasterUtils.GetLoginFromUsername(serviceDefinition.Credentials.Username)), RegistryRights.WriteKey, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
                        }

                        processInfo.SetAccessControl(rs);

                        #endregion
                    }
                }
            }
        }