Registry.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / Microsoft / Win32 / Registry.cs / 1305376 / Registry.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
#if FEATURE_WIN32_REGISTRY
 
namespace Microsoft.Win32 { 
    using System;
    using System.Runtime.InteropServices; 
    using System.Runtime.Versioning;

#if !FEATURE_PAL
 
    /**
     * Registry encapsulation. Contains members representing all top level system 
     * keys. 
     *
     * @security(checkClassLinking=on) 
     */
    //This class contains only static members and does not need to be serializable.
    [ComVisible(true)]
    public static class Registry { 
        [System.Security.SecuritySafeCritical]  // auto-generated
        static Registry() 
        { 
        }
 
        /**
         * Current User Key.
         *
         * This key should be used as the root for all user specific settings. 
         */
        [ResourceExposure(ResourceScope.Machine)] 
        public static readonly RegistryKey CurrentUser        = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER); 

        /** 
         * Local Machine Key.
         *
         * This key should be used as the root for all machine specific settings.
         */ 
        [ResourceExposure(ResourceScope.Machine)]
        public static readonly RegistryKey LocalMachine       = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE); 
 
        /**
         * Classes Root Key. 
         *
         * This is the root key of class information.
         */
        [ResourceExposure(ResourceScope.Machine)] 
        public static readonly RegistryKey ClassesRoot        = RegistryKey.GetBaseKey(RegistryKey.HKEY_CLASSES_ROOT);
 
        /** 
         * Users Root Key.
         * 
         * This is the root of users.
         */
        [ResourceExposure(ResourceScope.Machine)]
        public static readonly RegistryKey Users              = RegistryKey.GetBaseKey(RegistryKey.HKEY_USERS); 

        /** 
         * Performance Root Key. 
         *
         * This is where dynamic performance data is stored on NT. 
         */
        [ResourceExposure(ResourceScope.Machine)]
        public static readonly RegistryKey PerformanceData    = RegistryKey.GetBaseKey(RegistryKey.HKEY_PERFORMANCE_DATA);
 
        /**
         * Current Config Root Key. 
         * 
         * This is where current configuration information is stored.
         */ 
        [ResourceExposure(ResourceScope.Machine)]
        public static readonly RegistryKey CurrentConfig      = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_CONFIG);

        /** 
         * Dynamic Data Root Key.
         * 
         * LEGACY: This is where dynamic performance data is stored on Win9X. 
         * This does not exist on NT.
         */ 
        [ResourceExposure(ResourceScope.Machine)]
        [Obsolete("The DynData registry key only works on Win9x, which is no longer supported by the CLR.  On NT-based operating systems, use the PerformanceData registry key instead.")]
        public static readonly RegistryKey DynData            = RegistryKey.GetBaseKey(RegistryKey.HKEY_DYN_DATA);
 
        //
        // Following function will parse a keyName and returns the basekey for it. 
        // It will also store the subkey name in the out parameter. 
        // If the keyName is not valid, we will throw ArgumentException.
        // The return value shouldn't be null. 
        //
        [System.Security.SecurityCritical]  // auto-generated
        private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName) {
            if( keyName == null) { 
                throw new ArgumentNullException("keyName");
            } 
 
            string basekeyName;
            int i = keyName.IndexOf('\\'); 
            if( i != -1) {
                basekeyName = keyName.Substring(0, i).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
            }
            else { 
                basekeyName = keyName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
            } 
            RegistryKey basekey = null; 

            switch(basekeyName) { 
                case "HKEY_CURRENT_USER":
                    basekey = Registry.CurrentUser;
                    break;
                case "HKEY_LOCAL_MACHINE": 
                    basekey = Registry.LocalMachine;
                    break; 
                case "HKEY_CLASSES_ROOT": 
                    basekey = Registry.ClassesRoot;
                    break; 
                case "HKEY_USERS":
                    basekey = Registry.Users;
                    break;
                case "HKEY_PERFORMANCE_DATA": 
                    basekey = Registry.PerformanceData;
                    break; 
                case "HKEY_CURRENT_CONFIG": 
                    basekey = Registry.CurrentConfig;
                    break; 
                case "HKEY_DYN_DATA":
                    basekey = RegistryKey.GetBaseKey(RegistryKey.HKEY_DYN_DATA);
                    break;
                default: 
                    throw new ArgumentException(Environment.GetResourceString("Arg_RegInvalidKeyName", "keyName"));
            } 
            if( i == -1 || i == keyName.Length) { 
                subKeyName = string.Empty;
            } 
            else {
                subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1);
            }
            return basekey; 
        }
 
        [System.Security.SecuritySafeCritical]  // auto-generated 
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)] 
        public static object GetValue(string keyName, string valueName, object defaultValue ) {
            string subKeyName;
            RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
            BCLDebug.Assert(basekey != null, "basekey can't be null."); 
            RegistryKey key = basekey.OpenSubKey(subKeyName);
            if(key == null) { // if the key doesn't exist, do nothing 
                return null; 
            }
            try { 
                return key.GetValue(valueName, defaultValue);
            }
            finally {
                key.Close(); 
            }
        } 
 
        [System.Security.SecuritySafeCritical]  // auto-generated
        [ResourceExposure(ResourceScope.Machine)] 
        [ResourceConsumption(ResourceScope.Machine)]
        public static void SetValue(string keyName, string valueName, object value ) {
            SetValue(keyName, valueName, value, RegistryValueKind.Unknown);
        } 

        [System.Security.SecuritySafeCritical]  // auto-generated 
        [ResourceExposure(ResourceScope.Machine)] 
        [ResourceConsumption(ResourceScope.Machine)]
        public static void SetValue(string keyName, string valueName, object value, RegistryValueKind valueKind ) { 
            string subKeyName;
            RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
            BCLDebug.Assert(basekey != null, "basekey can't be null!");
            RegistryKey key = basekey.CreateSubKey(subKeyName); 
            BCLDebug.Assert(key != null, "An exception should be thrown if failed!");
            try { 
                key.SetValue(valueName, value, valueKind); 
            }
            finally { 
                key.Close();
            }
        }
    } 

#endif // !FEATURE_PAL 
} 
#endif // FEATURE_WIN32_REGISTRY
 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
#if FEATURE_WIN32_REGISTRY
 
namespace Microsoft.Win32 { 
    using System;
    using System.Runtime.InteropServices; 
    using System.Runtime.Versioning;

#if !FEATURE_PAL
 
    /**
     * Registry encapsulation. Contains members representing all top level system 
     * keys. 
     *
     * @security(checkClassLinking=on) 
     */
    //This class contains only static members and does not need to be serializable.
    [ComVisible(true)]
    public static class Registry { 
        [System.Security.SecuritySafeCritical]  // auto-generated
        static Registry() 
        { 
        }
 
        /**
         * Current User Key.
         *
         * This key should be used as the root for all user specific settings. 
         */
        [ResourceExposure(ResourceScope.Machine)] 
        public static readonly RegistryKey CurrentUser        = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER); 

        /** 
         * Local Machine Key.
         *
         * This key should be used as the root for all machine specific settings.
         */ 
        [ResourceExposure(ResourceScope.Machine)]
        public static readonly RegistryKey LocalMachine       = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE); 
 
        /**
         * Classes Root Key. 
         *
         * This is the root key of class information.
         */
        [ResourceExposure(ResourceScope.Machine)] 
        public static readonly RegistryKey ClassesRoot        = RegistryKey.GetBaseKey(RegistryKey.HKEY_CLASSES_ROOT);
 
        /** 
         * Users Root Key.
         * 
         * This is the root of users.
         */
        [ResourceExposure(ResourceScope.Machine)]
        public static readonly RegistryKey Users              = RegistryKey.GetBaseKey(RegistryKey.HKEY_USERS); 

        /** 
         * Performance Root Key. 
         *
         * This is where dynamic performance data is stored on NT. 
         */
        [ResourceExposure(ResourceScope.Machine)]
        public static readonly RegistryKey PerformanceData    = RegistryKey.GetBaseKey(RegistryKey.HKEY_PERFORMANCE_DATA);
 
        /**
         * Current Config Root Key. 
         * 
         * This is where current configuration information is stored.
         */ 
        [ResourceExposure(ResourceScope.Machine)]
        public static readonly RegistryKey CurrentConfig      = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_CONFIG);

        /** 
         * Dynamic Data Root Key.
         * 
         * LEGACY: This is where dynamic performance data is stored on Win9X. 
         * This does not exist on NT.
         */ 
        [ResourceExposure(ResourceScope.Machine)]
        [Obsolete("The DynData registry key only works on Win9x, which is no longer supported by the CLR.  On NT-based operating systems, use the PerformanceData registry key instead.")]
        public static readonly RegistryKey DynData            = RegistryKey.GetBaseKey(RegistryKey.HKEY_DYN_DATA);
 
        //
        // Following function will parse a keyName and returns the basekey for it. 
        // It will also store the subkey name in the out parameter. 
        // If the keyName is not valid, we will throw ArgumentException.
        // The return value shouldn't be null. 
        //
        [System.Security.SecurityCritical]  // auto-generated
        private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName) {
            if( keyName == null) { 
                throw new ArgumentNullException("keyName");
            } 
 
            string basekeyName;
            int i = keyName.IndexOf('\\'); 
            if( i != -1) {
                basekeyName = keyName.Substring(0, i).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
            }
            else { 
                basekeyName = keyName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
            } 
            RegistryKey basekey = null; 

            switch(basekeyName) { 
                case "HKEY_CURRENT_USER":
                    basekey = Registry.CurrentUser;
                    break;
                case "HKEY_LOCAL_MACHINE": 
                    basekey = Registry.LocalMachine;
                    break; 
                case "HKEY_CLASSES_ROOT": 
                    basekey = Registry.ClassesRoot;
                    break; 
                case "HKEY_USERS":
                    basekey = Registry.Users;
                    break;
                case "HKEY_PERFORMANCE_DATA": 
                    basekey = Registry.PerformanceData;
                    break; 
                case "HKEY_CURRENT_CONFIG": 
                    basekey = Registry.CurrentConfig;
                    break; 
                case "HKEY_DYN_DATA":
                    basekey = RegistryKey.GetBaseKey(RegistryKey.HKEY_DYN_DATA);
                    break;
                default: 
                    throw new ArgumentException(Environment.GetResourceString("Arg_RegInvalidKeyName", "keyName"));
            } 
            if( i == -1 || i == keyName.Length) { 
                subKeyName = string.Empty;
            } 
            else {
                subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1);
            }
            return basekey; 
        }
 
        [System.Security.SecuritySafeCritical]  // auto-generated 
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)] 
        public static object GetValue(string keyName, string valueName, object defaultValue ) {
            string subKeyName;
            RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
            BCLDebug.Assert(basekey != null, "basekey can't be null."); 
            RegistryKey key = basekey.OpenSubKey(subKeyName);
            if(key == null) { // if the key doesn't exist, do nothing 
                return null; 
            }
            try { 
                return key.GetValue(valueName, defaultValue);
            }
            finally {
                key.Close(); 
            }
        } 
 
        [System.Security.SecuritySafeCritical]  // auto-generated
        [ResourceExposure(ResourceScope.Machine)] 
        [ResourceConsumption(ResourceScope.Machine)]
        public static void SetValue(string keyName, string valueName, object value ) {
            SetValue(keyName, valueName, value, RegistryValueKind.Unknown);
        } 

        [System.Security.SecuritySafeCritical]  // auto-generated 
        [ResourceExposure(ResourceScope.Machine)] 
        [ResourceConsumption(ResourceScope.Machine)]
        public static void SetValue(string keyName, string valueName, object value, RegistryValueKind valueKind ) { 
            string subKeyName;
            RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
            BCLDebug.Assert(basekey != null, "basekey can't be null!");
            RegistryKey key = basekey.CreateSubKey(subKeyName); 
            BCLDebug.Assert(key != null, "An exception should be thrown if failed!");
            try { 
                key.SetValue(valueName, value, valueKind); 
            }
            finally { 
                key.Close();
            }
        }
    } 

#endif // !FEATURE_PAL 
} 
#endif // FEATURE_WIN32_REGISTRY
 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK