Ejemplo n.º 1
0
        /// <summary>
        /// Retrieves an array of strings containing all the subkey names.
        /// </summary>
        public string[] GetSubKeyNames()
        {
            EnsureNotDisposed();

            var names = new List <string>();

            char[] name = ArrayPool <char> .Shared.Rent(MaxKeyLength + 1);

            try
            {
                int result;
                int nameLength = name.Length;

                while ((result = Win32Native.RegEnumKeyEx(
                            _hkey,
                            names.Count,
                            name,
                            ref nameLength,
                            null,
                            null,
                            null,
                            null)) != Interop.Errors.ERROR_NO_MORE_ITEMS)
                {
                    switch (result)
                    {
                    case Interop.Errors.ERROR_SUCCESS:
                        names.Add(new string(name, 0, nameLength));
                        nameLength = name.Length;
                        break;

                    default:
                        // Throw the error
                        Win32Error(result, null);
                        break;
                    }
                }
            }
            finally
            {
                ArrayPool <char> .Shared.Return(name);
            }

            return(names.ToArray());
        }
Ejemplo n.º 2
0
        internal unsafe String[] InternalGetSubKeyNames()
        {
            EnsureNotDisposed();
            int subkeys = InternalSubKeyCount();

            String[] names = new String[subkeys];  // Returns 0-length array if empty.

            if (subkeys > 0)
            {
                char[] name = new char[MaxKeyLength + 1];

                int namelen;

                fixed(char *namePtr = &name[0])
                {
                    for (int i = 0; i < subkeys; i++)
                    {
                        namelen = name.Length; // Don't remove this. The API's doesn't work if this is not properly initialised.
                        int ret = Win32Native.RegEnumKeyEx(hkey,
                                                           i,
                                                           namePtr,
                                                           ref namelen,
                                                           null,
                                                           null,
                                                           null,
                                                           null);
                        if (ret != 0)
                        {
                            Win32Error(ret, null);
                        }
                        names[i] = new String(namePtr);
                    }
                }
            }

            return(names);
        }