Мне нужно получить список подразделов и список значений в ветви реестра.
[DllImport("advapi32.dll", EntryPoint="RegEnumKeyExW",
CallingConvention=CallingConvention.Winapi)]
[MethodImpl(MethodImplOptions.PreserveSig)]
extern private static int RegEnumKeyEx(IntPtr hkey, uint index,
char[] lpName, ref uint lpcbName,
IntPtr reserved, IntPtr lpClass, IntPtr lpcbClass,
out long lpftLastWriteTime);
// Get the names of all subkeys underneath this registry key.
public String[] GetSubKeyNames()
{
lock(this)
{
if(hKey != IntPtr.Zero)
{
// Get the number of subkey names under the key.
uint numSubKeys, numValues;
RegQueryInfoKey(hKey, null,IntPtr.Zero, IntPtr.Zero,out numSubKeys, IntPtr.Zero, IntPtr.Zero, out numValues,IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
// Create an array to hold the names.
String[] names = new String [numSubKeys];
StringBuilder sb = new StringBuilder();
uint MAX_REG_KEY_SIZE = 1024;
uint index = 0;
long writeTime;
while (index < numSubKeys)
{
sb = new StringBuilder();
if (RegEnumKeyEx(hKey,index,sb,ref MAX_REG_KEY_SIZE, IntPtr.Zero,IntPtr.Zero,IntPtr.Zero,out writeTime) != 0)
{
break;
}
names[(int)(index++)] = sb.ToString();
}
// Return the final name array to the caller.
return names;
}
return new String [0];
}
}
Теперь это работает хорошо, но только для первого элемента.Он возвращает имя ключа для 0-индекса, но для другого он возвращает "".
Как это может быть?
Кстати: я заменил свое определение на ваше, хорошо работает