Ejemplo n.º 1
0
        private static void ExampleRecurse(OffregKey key)
        {
            ValueContainer[] values = key.EnumerateValues();

            if (values.Length > 0)
            {
                Console.WriteLine("[" + key.FullName + "]");
                foreach (ValueContainer value in values)
                {
                    RegValueType type = value.Type;
                    object data = value.Data;
                    Console.WriteLine(value.Name + " (" + type + ")=" + data);
                }

                Console.WriteLine("");
            }

            SubKeyContainer[] subKeys = key.EnumerateSubKeys();

            foreach (SubKeyContainer subKey in subKeys)
            {
                try
                {
                    using (OffregKey sub = key.OpenSubKey(subKey.Name))
                    {
                        ExampleRecurse(sub);
                    }
                }
                catch (Win32Exception ex)
                {
                    Console.WriteLine("<" + key.FullName + " -> " + subKey.Name + ": " + ex.Message + ">");
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Internal recursive function.
        /// </summary>
        /// <param name="key"></param>
        private static void DeleteSubKeyTree(OffregKey key)
        {
            // Get childs
            string[] childs = key.GetSubKeyNames();

            // Delete all those childs
            foreach (string child in childs)
            {
                try
                {
                    using (OffregKey childKey = key.OpenSubKey(child))
                        DeleteSubKeyTree(childKey);
                }
                catch (Win32Exception ex)
                {
                    switch (ex.NativeErrorCode)
                    {
                    case (int)Win32Result.ERROR_FILE_NOT_FOUND:
                        // Child didn't exist
                        break;

                    default:
                        throw;
                    }
                }
            }

            // Delete self
            key.Delete();
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Internal constructor to form an Offline Registry Hive from an open handle.
        /// </summary>
        /// <param name="hivePtr"></param>
        internal OffregHive(IntPtr hivePtr)
        {
            _intPtr = hivePtr;

            // Represent this as a key also
            Root = new OffregKey(null, _intPtr, null);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Internal constructor to form an Offline Registry Hive from an open handle.
        /// </summary>
        /// <param name="hivePtr"></param>
        internal OffregHive(IntPtr hivePtr)
        {
            _intPtr = hivePtr;

            // Represent this as a key also
            Root = new OffregKey(null, _intPtr, null);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Constructor, uses an already-open pointer as a key.
        /// </summary>
        /// <param name="parent">The parent key.</param>
        /// <param name="ptr">Handle to an open key.</param>
        /// <param name="name">The name of the open key.</param>
        internal OffregKey(OffregKey parent, IntPtr ptr, string name)
        {
            _intPtr = ptr;

            Name = name;
            FullName = (parent == null || parent.FullName == null ? "" : parent.FullName + "\\") + name;
            _parent = parent;

            _metadata = new QueryInfoKeyData();
            RefreshMetadata();
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Constructor, uses an already-open pointer as a key.
        /// </summary>
        /// <param name="parent">The parent key.</param>
        /// <param name="ptr">Handle to an open key.</param>
        /// <param name="name">The name of the open key.</param>
        internal OffregKey(OffregKey parent, IntPtr ptr, string name)
        {
            _intPtr = ptr;

            Name     = name;
            FullName = (parent == null || parent.FullName == null ? "" : parent.FullName + "\\") + name;
            _parent  = parent;

            _metadata = new QueryInfoKeyData();
            RefreshMetadata();
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Recursively delete a subkey and all its subkeys.
        /// </summary>
        /// <param name="name">Name of the subkey to delete.</param>
        public void DeleteSubKeyTree(string name)
        {
            // Open key
            using (OffregKey subKey = OpenSubKey(name))
            {
                DeleteSubKeyTree(subKey);
            }

            // Refresh
            RefreshMetadata();
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Constructor, opens a subkey.
        /// </summary>
        /// <param name="parentKey">The parent key.</param>
        /// <param name="name">The name of the subkey to open.</param>
        internal OffregKey(OffregKey parentKey, string name)
        {
            Win32Result result = OffregNative.OpenKey(parentKey._intPtr, name, out _intPtr);

            if (result != Win32Result.ERROR_SUCCESS)
                throw new Win32Exception((int)result);

            Name = name;
            FullName = (parentKey.FullName == null ? "" : parentKey.FullName + "\\") + name;
            _parent = parentKey;

            _metadata = new QueryInfoKeyData();
            RefreshMetadata();
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Tries to opens a subkey.
        ///     Will handle multi-level names, such as "Software\SubKey\Subkey2\"
        /// </summary>
        /// <param name="name">Name of the subkey to open.</param>
        /// <param name="key">The newly opened subkey</param>
        /// <returns>True if the operation was sucessful, false otherwise.</returns>
        public bool TryOpenSubKey(string name, out OffregKey key)
        {
            IntPtr      childPtr;
            Win32Result result = OffregNative.OpenKey(_intPtr, name, out childPtr);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                key = null;
                return(false);
            }

            key = new OffregKey(this, childPtr, name);
            return(true);
        }
Ejemplo n.º 10
0
        /// <summary>
        ///     Constructor, opens a subkey.
        /// </summary>
        /// <param name="parentKey">The parent key.</param>
        /// <param name="name">The name of the subkey to open.</param>
        internal OffregKey(OffregKey parentKey, string name)
        {
            Win32Result result = OffregNative.OpenKey(parentKey._intPtr, name, out _intPtr);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            Name     = name;
            FullName = (parentKey.FullName == null ? "" : parentKey.FullName + "\\") + name;
            _parent  = parentKey;

            _metadata = new QueryInfoKeyData();
            RefreshMetadata();
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Creates a new subkey (or opens an existing one).
        /// </summary>
        /// <param name="name">The name of the subkey to create (or open).</param>
        /// <param name="options">Key creation options.</param>
        /// <returns>The newly created (or opened) key.</returns>
        public OffregKey CreateSubKey(string name, RegOption options = 0)
        {
            IntPtr         newKeyPtr;
            KeyDisposition disposition;
            Win32Result    result = OffregNative.CreateKey(_intPtr, name, null, options, IntPtr.Zero, out newKeyPtr,
                                                           out disposition);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            // Return new key
            OffregKey newKey = new OffregKey(this, newKeyPtr, name);

            RefreshMetadata();

            return(newKey);
        }
Ejemplo n.º 12
0
        /// <summary>
        ///     Internal recursive function.
        /// </summary>
        /// <param name="key"></param>
        private static void DeleteSubKeyTree(OffregKey key)
        {
            // Get childs
            string[] childs = key.GetSubKeyNames();

            // Delete all those childs
            foreach (string child in childs)
            {
                try
                {
                    using (OffregKey childKey = key.OpenSubKey(child))
                        DeleteSubKeyTree(childKey);
                }
                catch (Win32Exception ex)
                {
                    switch (ex.NativeErrorCode)
                    {
                        case (int)Win32Result.ERROR_FILE_NOT_FOUND:
                            // Child didn't exist
                            break;
                        default:
                            throw;
                    }
                }
            }

            // Delete self
            key.Delete();
        }
Ejemplo n.º 13
0
        /// <summary>
        ///     Tries to opens a subkey.
        ///     Will handle multi-level names, such as "Software\SubKey\Subkey2\"
        /// </summary>
        /// <param name="name">Name of the subkey to open.</param>
        /// <param name="key">The newly opened subkey</param>
        /// <returns>True if the operation was sucessful, false otherwise.</returns>
        public bool TryOpenSubKey(string name, out OffregKey key)
        {
            IntPtr childPtr;
            Win32Result result = OffregNative.OpenKey(_intPtr, name, out childPtr);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                key = null;
                return false;
            }

            key = new OffregKey(this, childPtr, name);
            return true;
        }
Ejemplo n.º 14
0
        /// <summary>
        ///     Creates a new subkey (or opens an existing one).
        /// </summary>
        /// <param name="name">The name of the subkey to create (or open).</param>
        /// <param name="options">Key creation options.</param>
        /// <returns>The newly created (or opened) key.</returns>
        public OffregKey CreateSubKey(string name, RegOption options = 0)
        {
            IntPtr newKeyPtr;
            KeyDisposition disposition;
            Win32Result result = OffregNative.CreateKey(_intPtr, name, null, options, IntPtr.Zero, out newKeyPtr,
                                                        out disposition);

            if (result != Win32Result.ERROR_SUCCESS)
                throw new Win32Exception((int)result);

            // Return new key
            OffregKey newKey = new OffregKey(this, newKeyPtr, name);

            RefreshMetadata();

            return newKey;
        }
Ejemplo n.º 15
0
 public void Initiate()
 {
     _hive = OffregHive.Create();
     _key = _hive.Root.CreateSubKey("A");
 }
Ejemplo n.º 16
0
 public void Initiate()
 {
     _hive = OffregHive.Create();
     _key = _hive.Root;
 }