Exemple #1
0
        /// <summary>
        /// Acctually read a registry value. Requires knowledge of the
        /// value's type and size.
        /// </summary>
        public object GetValue(RegistryKey rkey, string name, object defaultValue, RegistryValueOptions options)
        {
            RegistryValueKind type = 0;
            int    size            = 0;
            object obj             = null;
            IntPtr handle          = GetHandle(rkey);
            int    result          = RegQueryValueEx(handle, name, IntPtr.Zero, ref type, IntPtr.Zero, ref size);

            if (result == Win32ResultCode.FileNotFound || result == Win32ResultCode.MarkedForDeletion)
            {
                return(defaultValue);
            }

            if (result != Win32ResultCode.MoreData && result != Win32ResultCode.Success)
            {
                GenerateException(result);
            }

            if (type == RegistryValueKind.String)
            {
                byte[] data;
                result = GetBinaryValue(rkey, name, type, out data, size);
                obj    = RegistryKey.DecodeString(data);
            }
            else if (type == RegistryValueKind.ExpandString)
            {
                byte [] data;
                result = GetBinaryValue(rkey, name, type, out data, size);
                obj    = RegistryKey.DecodeString(data);
                if ((options & RegistryValueOptions.DoNotExpandEnvironmentNames) == 0)
                {
                    obj = Environment.ExpandEnvironmentVariables((string)obj);
                }
            }
            else if (type == RegistryValueKind.DWord)
            {
                int data = 0;
                result = RegQueryValueEx(handle, name, IntPtr.Zero, ref type, ref data, ref size);
                obj    = data;
            }
            else if (type == RegistryValueKind.QWord)
            {
                long data = 0;
                result = RegQueryValueEx(handle, name, IntPtr.Zero, ref type, ref data, ref size);
                obj    = data;
            }
            else if (type == RegistryValueKind.Binary)
            {
                byte[] data;
                result = GetBinaryValue(rkey, name, type, out data, size);
                obj    = data;
            }
            else if (type == RegistryValueKind.MultiString)
            {
                obj = null;
                byte[] data;
                result = GetBinaryValue(rkey, name, type, out data, size);

                if (result == Win32ResultCode.Success)
                {
                    obj = RegistryKey.DecodeString(data).Split('\0');
                }
            }
            else
            {
                // should never get here
                throw new SystemException();
            }

            // check result codes again:
            if (result != Win32ResultCode.Success)
            {
                GenerateException(result);
            }


            return(obj);
        }
        public object GetValue(RegistryKey rkey, string name, object defaultValue, RegistryValueOptions options)
        {
            RegistryValueKind registryValueKind = RegistryValueKind.Unknown;
            int    size   = 0;
            IntPtr handle = Win32RegistryApi.GetHandle(rkey);
            int    num    = Win32RegistryApi.RegQueryValueEx(handle, name, IntPtr.Zero, ref registryValueKind, IntPtr.Zero, ref size);

            if (num == 2 || num == 1018)
            {
                return(defaultValue);
            }
            if (num != 234 && num != 0)
            {
                this.GenerateException(num);
            }
            object obj;

            if (registryValueKind == RegistryValueKind.String)
            {
                byte[] data;
                num = this.GetBinaryValue(rkey, name, registryValueKind, out data, size);
                obj = RegistryKey.DecodeString(data);
            }
            else if (registryValueKind == RegistryValueKind.ExpandString)
            {
                byte[] data2;
                num = this.GetBinaryValue(rkey, name, registryValueKind, out data2, size);
                obj = RegistryKey.DecodeString(data2);
                if ((options & RegistryValueOptions.DoNotExpandEnvironmentNames) == RegistryValueOptions.None)
                {
                    obj = Environment.ExpandEnvironmentVariables((string)obj);
                }
            }
            else if (registryValueKind == RegistryValueKind.DWord)
            {
                int num2 = 0;
                num = Win32RegistryApi.RegQueryValueEx(handle, name, IntPtr.Zero, ref registryValueKind, ref num2, ref size);
                obj = num2;
            }
            else if (registryValueKind == RegistryValueKind.Binary)
            {
                byte[] array;
                num = this.GetBinaryValue(rkey, name, registryValueKind, out array, size);
                obj = array;
            }
            else
            {
                if (registryValueKind != RegistryValueKind.MultiString)
                {
                    throw new SystemException();
                }
                obj = null;
                byte[] data3;
                num = this.GetBinaryValue(rkey, name, registryValueKind, out data3, size);
                if (num == 0)
                {
                    obj = RegistryKey.DecodeString(data3).Split(new char[1]);
                }
            }
            if (num != 0)
            {
                this.GenerateException(num);
            }
            return(obj);
        }