Example #1
0
        /// <summary>
        /// Opens and returns the RegistryKey found at the given partial registry path, starting at the given startAtKey.  Requests the given permissions.
        /// </summary>
        /// <param name="startAtKey">Gives the RegistryKey instance to start the relative traversal at, or null if the traversal should start at the root.</param>
        /// <param name="keyPathArray">Gives the partial registry key path to traverse starting at the given key, in array form.</param>
        /// <param name="permissions">Gives the RegistryKeyPermissionCheck value for the requested permissions.</param>
        /// <returns>The RegistryKey instance for the requested path.</returns>
        /// <exception cref="System.ArgumentException">If the requested regKeyPath is not valid or cannot be opened</exception>
        public static RegistryKey OpenRegistryKeyPath(this RegistryKey startAtKey, string[] keyPathArray, RegistryKeyPermissionCheck permissions)
        {
            RegistryKey currentKey = startAtKey;
            bool preventDisposeCurrentKey = true;       // it came from the one we were given on call

            try
            {
                RegistryKey nextKey = null;

                if (keyPathArray == null)
                    throw new System.ArgumentNullException("keyPathArray");
                else if (keyPathArray.Length == 0)
                    throw new System.ArgumentException("must have at least one element", "keyPathArray");

                int keyIdx = 0;

                if (currentKey == null)
                {
                    currentKey = GetRegistryHiveKey(keyPathArray[keyIdx++]);
                    preventDisposeCurrentKey = true;        // it came from the GetRegistryHiveKey method (static key)
                }

                for (; keyIdx < keyPathArray.Length; keyIdx++)
                {
                    string keyName = keyPathArray[keyIdx];

                    if (permissions == RegistryKeyPermissionCheck.ReadWriteSubTree)
                    {
                        nextKey = currentKey.OpenSubKey(keyName, permissions);
                        if (nextKey == null)
                            nextKey = currentKey.CreateSubKey(keyName, permissions);
                        if (nextKey == null)
                            throw new System.ArgumentException(Utils.Fcns.CheckedFormat("Unable to create key:{0} under path:{1}, {2}", keyName, currentKey.ToString(), permissions.ToString()), Utils.Fcns.CheckedFormat("keyPathArray[{0}]", keyIdx));
                    }
                    else
                    {
                        nextKey = currentKey.OpenSubKey(keyName, permissions);
                        if (nextKey == null)
                            throw new System.ArgumentException(Utils.Fcns.CheckedFormat("Unable to open key:{0} under path:{1}, {2}", keyName, currentKey.ToString(), permissions.ToString()), Utils.Fcns.CheckedFormat("keyPathArray[{0}]", keyIdx));
                    }

                    if (!preventDisposeCurrentKey)
                        Utils.Fcns.DisposeOfObject(ref currentKey);

                    currentKey = nextKey;
                    preventDisposeCurrentKey = false;       // it comes from one that was opened above so we can dispose it if it is not the one we will actually return.
                }

                return currentKey;
            }
            catch
            {
                // will re-throw the original exception after disposing of any intermediate key
                if (!preventDisposeCurrentKey && currentKey != null)
                    Utils.Fcns.DisposeOfObject(ref currentKey);

                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Opens and returns the RegistryKey found at the given partial registry path, starting at the given startAtKey.  Requests the given permissions.
        /// </summary>
        /// <param name="startAtKey">Gives the RegistryKey instance to start the relative traversal at, or null if the traversal should start at the root.</param>
        /// <param name="keyPathArray">Gives the partial registry key path to traverse starting at the given key, in array form.</param>
        /// <param name="permissions">Gives the RegistryKeyPermissionCheck value for the requested permissions.</param>
        /// <returns>The RegistryKey instance for the requested path.</returns>
        /// <exception cref="System.ArgumentException">If the requested regKeyPath is not valid or cannot be opened</exception>
        public static RegistryKey OpenRegistryKeyPath(this RegistryKey startAtKey, string[] keyPathArray, RegistryKeyPermissionCheck permissions)
        {
            RegistryKey currentKey = startAtKey;
            bool        preventDisposeCurrentKey = true; // it came from the one we were given on call

            try
            {
                RegistryKey nextKey = null;

                if (keyPathArray == null)
                {
                    throw new System.ArgumentNullException("keyPathArray");
                }
                else if (keyPathArray.Length == 0)
                {
                    throw new System.ArgumentException("must have at least one element", "keyPathArray");
                }

                int keyIdx = 0;

                if (currentKey == null)
                {
                    currentKey = GetRegistryHiveKey(keyPathArray[keyIdx++]);
                    preventDisposeCurrentKey = true;        // it came from the GetRegistryHiveKey method (static key)
                }

                for (; keyIdx < keyPathArray.Length; keyIdx++)
                {
                    string keyName = keyPathArray[keyIdx];

                    if (permissions == RegistryKeyPermissionCheck.ReadWriteSubTree)
                    {
                        nextKey = currentKey.OpenSubKey(keyName, permissions);
                        if (nextKey == null)
                        {
                            nextKey = currentKey.CreateSubKey(keyName, permissions);
                        }
                        if (nextKey == null)
                        {
                            throw new System.ArgumentException(Utils.Fcns.CheckedFormat("Unable to create key:{0} under path:{1}, {2}", keyName, currentKey.ToString(), permissions.ToString()), Utils.Fcns.CheckedFormat("keyPathArray[{0}]", keyIdx));
                        }
                    }
                    else
                    {
                        nextKey = currentKey.OpenSubKey(keyName, permissions);
                        if (nextKey == null)
                        {
                            throw new System.ArgumentException(Utils.Fcns.CheckedFormat("Unable to open key:{0} under path:{1}, {2}", keyName, currentKey.ToString(), permissions.ToString()), Utils.Fcns.CheckedFormat("keyPathArray[{0}]", keyIdx));
                        }
                    }

                    if (!preventDisposeCurrentKey)
                    {
                        Utils.Fcns.DisposeOfObject(ref currentKey);
                    }

                    currentKey = nextKey;
                    preventDisposeCurrentKey = false;       // it comes from one that was opened above so we can dispose it if it is not the one we will actually return.
                }

                return(currentKey);
            }
            catch
            {
                // will re-throw the original exception after disposing of any intermediate key
                if (!preventDisposeCurrentKey && currentKey != null)
                {
                    Utils.Fcns.DisposeOfObject(ref currentKey);
                }

                throw;
            }
        }