If you're trying to do registry stuff on a remote system, you may not have sufficient privileges. I would break your code down into parts, all contained in a
try/catch
block.
Is there an inner exception?
BTW, I found this with google:
public static string ConnectToRegistry (string servername)
{
string regKeyToGet, keyToRead;
ConnectionOptions oConn = new ConnectionOptions();
oConn.Username = "admin";
oConn.Password = "pass";
ManagementScope scope = new ManagementScope(@"//" + servername +
@"/root/default",
oConn);
registry = new ManagementClass(scope,
new ManagementPath("StdRegProv"),
null);
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue");
inParams["sSubKeyName"] = regKeyToGet;
inParams["sValueName"] = keyToRead;
ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue", inParams, null);
return outParams["sValue"].ToString();
}