Reading Registry Key

I am working on a C# application that can take a computer name and connect to that computer over the server and check several of the local security policies and create a log of what policies are incorrectly set. In a nutshell, it is an automated way of going through a security checklist rather than manually looking at each policy at each computer. I am currently trying to read the "Audit Policies" such as "Audit Policy Change" etc. I found a registry key located in HKEY_LOCAL_MACHINE\\Security\\Policy\\PolAdtEv named "(Default)" that is a hexadecimal value that represents the setting of each of the 9 audit policies. Exactly what I need!! There is only one problem. The code below shows how I am trying to read this key. I have read that GetValue("") will read keys named "(Default)," I was also told to try null and "@". I tried all of them and am still having the same problem. ans in the code below always has an undefined value after the GetValue executes. I think the problem is that the (Default) is a REG_NONE type. To test this theory I used the registry editor to copy the (Default) and made it a REG_BINARY and named it "mine." I tried the same code again instead using rk.GetValue("mine"). Now the code works and I get the list of hex numbers that I need to determine the policies. Does anyone know why it will not read the (Default) key? Is it because it is a REG_NONE? Is there a way around this problem? I can't go to each computer and copy this key and change the type each time I want to run the program (well i could but I shouldn't have to). I would appreciate any help you can give. Thanks in advance. Sorry so lengthy.



RegistryKey rk;
string
remotename;
remotename = "pc0057963";
string
subkey = "Security\\Policy\\PolAdtEv";
rk = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,remotename)
.OpenSubKey(subkey);

//Print the values.
listBox1.Items.Add("There are " + rk.ValueCount.ToString() + " values for " + rk.Name.ToString() + ".");
//string val = (string)(rk.GetValue(null));

//byte[] ans = (byte[])rk.GetValue("")//ans = <undefined value>
byte
[] ans = (byte[])rk.GetValue("mine");//ans = 01 17 F5 77 03 00 00 00 03..

for
(int x=0;x<ans.Length;x++)
{
listBox2.Items.Add(String.Format("{0:x2}",ans[x]));
}

[3451 byte] By [Justin5] at [2007-12-16]
# 1
When I open regedit, it shows default values as "REG_SZ". That would make your cast invalid. I tried the simple code below and it worked just fine, so you might want to try creating a temporary variable of type "object" to catch the return value, then examine that in the debugger.


RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\MyCompany\DatabaseConfigs\BW10\Connection1");
object value = rk.GetValue("");
System.
Console.WriteLine(value);

dkocur2 at 2007-9-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Yeah I noticed that some of my Defaults in other locations were REG_SZ too but the ones in HKEY_LOCAL_MACHINE\Security\Policy are REG_NONE. I tried declaring ans as an object but it still has and undefined value after calling GetValue(""). If you look at the help on GetValue it says it can read DWORD, REG_SZ, and REG_BINARY, so I am guessing I need to figure out another way to retrieve this key. I have looked at RegQueryValue some but haven't had much luck there. If anyone knows of another way to do this I would appreciate the help. Thanks.
Justin5 at 2007-9-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
I tested it with RegQueryValue in C++. Read and wrote REG_NONE values no problem. That may be the way to go.
dkocur2 at 2007-9-8 > top of Msdn Tech,Visual C#,Visual C# General...