Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List the data of a registry key

Status
Not open for further replies.

destroyhead

Programmer
Feb 22, 2006
27
0
0
GB
Hi everybody,
I need to have a list of all the subkeys and data (with the type) of a specified key.

I know how to list the subkeys :

string Machine = mymachine;
string KeyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Acrobat Reader\6.0\Installer"; //for example

//Creation of the ConnectionOptions
ConnectionOptions aConnectionOptions = new ConnectionOptions();
aConnectionOptions.Username = @"mydomain\myusername";
aConnectionOptions.Password = "mypassword";

// Choice of the Registry Hive
uint RegistryHive = 0;
string [] split = KeyPath.Split('\\');
switch (split[0])
{
case "CLASSES_ROOT":
RegistryHive = unchecked((uint)0x80000000);
break;
case "HKEY_CURRENT_USER":
RegistryHive = unchecked((uint)0x80000001);
break;
case "HKEY_LOCAL_MACHINE":
RegistryHive = unchecked((uint)0x80000002);
break;
case "HKEY_USERS":
RegistryHive = unchecked((uint)0x80000003);
break;
case "HKEY_PERFORMANCE_DATA":
RegistryHive = unchecked((uint)0x80000004);
break;
case "HKEY_CURRENT_CONFIG":
RegistryHive = unchecked((uint)0x80000005);
break;
case "HKEY_DYN_DATA":
RegistryHive = unchecked((uint)0x80000006);
break;
}

//Delete the registry hive to have a short key path
int i = 0;
string ShortKeyPath = "";
for (i = 1; i < split.Length - 1; i++)
ShortKeyPath += split + @"\";
ShortKeyPath += split;

ManagementScope aManagementScope = new ManagementScope(@"\\" + Machine + @"\root\default", aConnectionOptions);
ManagementPath aManagementPath = new ManagementPath("StdRegProv");
ManagementClass wmiRegistry = new ManagementClass(aManagementScope, aManagementPath, null);

//Enumerate subkeys

div1.InnerHtml = "<b>Subkeys :</b><br />";
object[] methodArgs = new object[] { RegistryHive, ShortKeyPath, null };
uint returnValue = (uint)wmiRegistry.InvokeMethod("EnumKey", methodArgs);

if (null != methodArgs[2])
{
string[] subKeys = methodArgs[2] as String[];
if (subKeys == null) return;
ManagementBaseObject inParam = wmiRegistry.GetMethodParameters("GetStringValue");
inParam["hDefKey"] = RegistryHive;
foreach (string subKey in subKeys)
{
div1.InnerHtml += subKey + "<br />";
}
}

I know how to retrieve a data with a specified type :

//Enumerate data
div1.InnerHtml += "<b>Data :</b><br />";
ManagementBaseObject inParam = wmiRegistry.GetMethodParameters("GetStringValue");
inParam["hDefKey"] = RegistryHive;
inParam["sSubKeyName"] = ShortKeyPath;
inParam["sValueName"] = "VersionMax"; //for example
ManagementBaseObject outParam = wmiRegistry.InvokeMethod("GetDWORDValue", inParam, null); //or "GetStringValue"
if ((uint)outParam["ReturnValue"] == 0)
div1.InnerHtml += outParam["uValue"].ToString() + "<br />"; //or "sValue"

What I would like to know is how to list the data and their types. I think that StdRegProv.EnumValues is doing nearly this.
But :
- the name of each data has to be specified in an array
- the type of each data has to be specified in an array

How do I loop through a key to find all the data and their types?

Thanks a lot for any help.

Regards.

Olivier
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top