public static void WriteValue(RegistryRootKeys rootKey, string keyPath,
                                      string valueName, object value, bool createIfNotExist)
        {
            RegistryKey regKey = GetRegistryRootKey(rootKey);

            string[]    pathToken = keyPath.Split('\\');
            RegistryKey subKey    = null;

            for (int i = 0; i < pathToken.Length; i++)
            {
                if (regKey != null)
                {
                    subKey = regKey.OpenSubKey(pathToken[i], true);
                    if (subKey == null && createIfNotExist)
                    {
                        subKey = regKey.CreateSubKey(pathToken[i]);
                    }
                    regKey = subKey;
                }
            }
            if (regKey != null)
            {
                regKey.SetValue(valueName, value);
            }
            else
            {
                throw new ApplicationException("Cannot create " + rootKey.ToString() + "\\" + keyPath);
            }
        }
Example #2
0
        private static RegistryKey GetRegistryRootKey(RegistryRootKeys pRootKey)
        {
            RegistryKey registryKey = (RegistryKey)null;

            switch (pRootKey)
            {
            case RegistryRootKeys.HKEY_CLASSES_ROOT:
                registryKey = Registry.ClassesRoot;
                break;

            case RegistryRootKeys.HKEY_CURRENT_CONFIG:
                registryKey = Registry.CurrentConfig;
                break;

            case RegistryRootKeys.HKEY_CURRENT_USER:
                registryKey = Registry.CurrentUser;
                break;

            case RegistryRootKeys.HKEY_LOCAL_MACHINE:
                registryKey = Registry.LocalMachine;
                break;

            case RegistryRootKeys.HKEY_PERFORMANCE_DATA:
                registryKey = Registry.PerformanceData;
                break;

            case RegistryRootKeys.HKEY_USERS:
                registryKey = Registry.Users;
                break;
            }
            return(registryKey);
        }
 private static RegistryKey GetRegistryRootKey(RegistryRootKeys rootKey)
 {
     RegistryKey regKey = null;
     switch (rootKey)
     {
         case RegistryRootKeys.HKEY_CLASSES_ROOT:
             regKey = Registry.ClassesRoot;
             break;
         case RegistryRootKeys.HKEY_CURRENT_CONFIG:
             regKey = Registry.CurrentConfig;
             break;
         case RegistryRootKeys.HKEY_CURRENT_USER:
             regKey = Registry.CurrentUser;
             break;
         case RegistryRootKeys.HKEY_LOCAL_MACHINE:
             regKey = Registry.LocalMachine;
             break;
         case RegistryRootKeys.HKEY_PERFORMANCE_DATA:
             regKey = Registry.PerformanceData;
             break;
         case RegistryRootKeys.HKEY_USERS:
             regKey = Registry.Users;
             break;
     }
     return regKey;
 }
Example #4
0
        /* Methode zum Löschen eines Schlüssels in der Registry */
        public static void DeleteKey(RegistryRootKeys rootKey, string keyPath)
        {
            // Den Pfad zum übergeordneten Schlüssel und den Namen des zu löschenden
            // Schlüssels ermitteln
            int    i             = keyPath.LastIndexOf("\\");
            string parentKeyPath = keyPath.Substring(0, i);
            string keyName       = keyPath.Substring(i + 1, keyPath.Length - i - 1);

            // Den dem zu löschenden Schlüssel übergeordneten Schlüssel zum Schreiben
            // öffnen
            RegistryKey regKey = GetRegistryRootKey(rootKey).OpenSubKey(
                parentKeyPath, true);

            if (regKey != null)
            {
                // Schlüssel über den übergeordneten Schlüssel löschen
                regKey.DeleteSubKey(keyName);
            }
            else
            {
                // Schlüssel nicht gefunden: Ausnahme werfen
                throw new Exception("Schlüssel " + rootKey.ToString() + "\\" +
                                    keyPath + " nicht gefunden");
            }
        }
 public static void WriteValue(RegistryRootKeys rootKey, string keyPath,
     string valueName, object value, bool createIfNotExist)
 {
     RegistryKey regKey = GetRegistryRootKey(rootKey);
     string[] pathToken = keyPath.Split('\\');
     RegistryKey subKey = null;
     for (int i = 0; i < pathToken.Length; i++)
     {
         if (regKey != null)
         {
             subKey = regKey.OpenSubKey(pathToken[i], true);
             if (subKey == null && createIfNotExist)
             {
                 subKey = regKey.CreateSubKey(pathToken[i]);
             }
             regKey = subKey;
         }
     }
     if (regKey != null)
     {
         regKey.SetValue(valueName, value);
     }
     else
     {
         throw new ApplicationException("Cannot create " + rootKey.ToString() + "\\" + keyPath);
     }
 }
Example #6
0
        public static void DeleteValue(RegistryRootKeys pRootKey, string pKeyPath, string valueName)
        {
            RegistryKey registryKey = RegistryAccess.GetRegistryRootKey(pRootKey).OpenSubKey(pKeyPath, true);

            if (registryKey == null)
            {
                throw new Exception(string.Format((IFormatProvider)CultureInfo.InvariantCulture, RegistryAccessStrings.DeleteValueError, (object)pKeyPath, (object)pRootKey.ToString()));
            }
            registryKey.DeleteValue(valueName, false);
        }
Example #7
0
        public static object ReadValue(RegistryRootKeys pRootKey, string pKeyPath, string pValueName, object pDefaultValue)
        {
            RegistryKey registryKey = RegistryAccess.GetRegistryRootKey(pRootKey).OpenSubKey(pKeyPath);
            object      obj         = pDefaultValue;

            if (registryKey != null)
            {
                obj = registryKey.GetValue(pValueName);
            }
            return(obj);
        }
Example #8
0
        public static void DeleteKey(RegistryRootKeys pRootKey, string pKeyPath)
        {
            int         length      = pKeyPath.LastIndexOf("\\");
            string      name        = pKeyPath.Substring(0, length);
            string      subkey      = pKeyPath.Substring(length + 1, pKeyPath.Length - length - 1);
            RegistryKey registryKey = RegistryAccess.GetRegistryRootKey(pRootKey).OpenSubKey(name, true);

            if (registryKey == null)
            {
                throw new Exception(string.Format((IFormatProvider)CultureInfo.InvariantCulture, RegistryAccessStrings.DeleteKeyError, (object)pKeyPath, (object)pRootKey.ToString()));
            }
            registryKey.DeleteSubKey(subkey);
        }
 public static object ReadValue(RegistryRootKeys rootKey, string keyPath,
     string valueName, object defaultValue)
 {
     RegistryKey regKey = GetRegistryRootKey(rootKey).OpenSubKey(@keyPath);
     object regValue = defaultValue;
     if (regKey != null)
     {
         regValue = regKey.GetValue(valueName);
     }
     else
     {
         WriteValue(rootKey, keyPath, valueName, defaultValue, true);
     }
     return regValue;
 }
        public static object ReadValue(RegistryRootKeys rootKey, string keyPath,
                                       string valueName, object defaultValue)
        {
            RegistryKey regKey   = GetRegistryRootKey(rootKey).OpenSubKey(@keyPath);
            object      regValue = defaultValue;

            if (regKey != null)
            {
                regValue = regKey.GetValue(valueName);
            }
            else
            {
                WriteValue(rootKey, keyPath, valueName, defaultValue, true);
            }
            return(regValue);
        }
Example #11
0
        /* Methode zum Lesen eines Registry-Eintrags */
        public static object ReadValue(RegistryRootKeys rootKey, string keyPath,
                                       string valueName, object defaultValue)
        {
            // Schlüssel ermitteln
            RegistryKey regKey = GetRegistryRootKey(rootKey).OpenSubKey(keyPath);

            // Wert auslesen, wenn der Unterschlüssel gefunden wurde
            object regValue = defaultValue;

            if (regKey != null)
            {
                regValue = regKey.GetValue(valueName);
            }

            return(regValue);
        }
Example #12
0
        /* Methode zum Löschen eines Werts in der Registry */
        public static void DeleteValue(RegistryRootKeys rootKey, string keyPath,
                                       string valueName)
        {
            // Unterschlüssel zum Schreiben öffnen
            RegistryKey regKey = GetRegistryRootKey(rootKey).OpenSubKey(
                keyPath, true);

            // Wert über den übergeordneten Schlüssel löschen
            // wenn dieser gefunden wurde
            if (regKey != null)
            {
                regKey.DeleteValue(valueName, false);
            }
            else
            {
                // Schlüssel nicht gefunden: Ausnahme werfen
                throw new Exception("Schlüssel " + rootKey.ToString() + "\\" +
                                    keyPath + " nicht gefunden");
            }
        }
Example #13
0
        /*  Methode zum Schreiben eines Registry-Eintrags */
        public static void WriteValue(RegistryRootKeys rootKey, string keyPath,
                                      string valueName, object value, bool createIfNotExist)
        {
            // Wurzel-Schlüssel ermitteln
            RegistryKey regKey = GetRegistryRootKey(rootKey);

            // Den Pfad in seine Einzelteile zerlegen
            string[] pathToken = keyPath.Split('\\');

            // Pfad durchgehen und den Schlüssel referenzieren
            RegistryKey subKey = null;

            for (int i = 0; i < pathToken.Length; i++)
            {
                if (regKey != null)
                {
                    // Unterschlüssel zum Lesen und Schreiben öffnen
                    subKey = regKey.OpenSubKey(pathToken[i], true);

                    if (subKey == null && createIfNotExist)
                    {
                        // Unterschlüssel nicht gefunden: Schlüssel erzeugen
                        // falls dies gewünscht ist
                        subKey = regKey.CreateSubKey(pathToken[i]);
                    }
                    regKey = subKey;
                }
            }

            // Wert schreiben, wenn der Unterschlüssel gefunden wurde
            if (regKey != null)
            {
                regKey.SetValue(valueName, value);
            }
            else
            {
                // Ausnahme werfen
                throw new Exception("Schlüssel " + rootKey.ToString() + "\\" +
                                    keyPath + " nicht gefunden");
            }
        }
Example #14
0
        private static RegistryKey GetRegistryRootKey(RegistryRootKeys rootKey)
        {
            // Ermitteln des Registry-Wurzel-Schlüssels
            RegistryKey regKey = null;

            switch (rootKey)
            {
            case RegistryRootKeys.HKEY_CLASSES_ROOT:
                regKey = Registry.ClassesRoot;
                break;

            case RegistryRootKeys.HKEY_CURRENT_CONFIG:
                regKey = Registry.CurrentConfig;
                break;

            case RegistryRootKeys.HKEY_CURRENT_USER:
                regKey = Registry.CurrentUser;
                break;

            case RegistryRootKeys.HKEY_DYN_DATA:
                regKey = Registry.DynData;
                break;

            case RegistryRootKeys.HKEY_LOCAL_MACHINE:
                regKey = Registry.LocalMachine;
                break;

            case RegistryRootKeys.HKEY_PERFORMANCE_DATA:
                regKey = Registry.PerformanceData;
                break;

            case RegistryRootKeys.HKEY_USERS:
                regKey = Registry.Users;
                break;
            }

            return(regKey);
        }
Example #15
0
        public static void WriteValue(RegistryRootKeys pRootKey, string pKeyPath, string valueName, object pValue, bool pCreateIfNotExist)
        {
            RegistryKey registryKey1 = RegistryAccess.GetRegistryRootKey(pRootKey);

            string[] strArray = pKeyPath.Split('\\');
            for (int index = 0; index < strArray.Length; ++index)
            {
                if (registryKey1 != null)
                {
                    RegistryKey registryKey2 = registryKey1.OpenSubKey(strArray[index], true);
                    if (registryKey2 == null & pCreateIfNotExist)
                    {
                        registryKey2 = registryKey1.CreateSubKey(strArray[index]);
                    }
                    registryKey1 = registryKey2;
                }
            }
            if (registryKey1 == null)
            {
                throw new Exception(string.Format((IFormatProvider)CultureInfo.InvariantCulture, RegistryAccessStrings.WriteValueError, (object)pKeyPath, (object)pRootKey.ToString()));
            }
            registryKey1.SetValue(valueName, pValue);
        }