getting a registry key and value in C# -
getting a registry key and value in C# -
sorry if simple, haven't coded since college. i'm trying write programme view registry entries in windows 7. want check see if registry value exists first, check see value is. if doesn't exist, want 1 message, if exist, want 1 message reflecting value of 1, , reflecting value of 0. got code work if registry key doesn't exist, if add together key , value crashes. not sure i'm doing wrong here. suggestions appreciated. here code.
using (registrykey key = registry.localmachine.opensubkey(@"system\currentcontrolset\services\lanmanserver\parameters")) if (key != null) { string val = (string)key.getvalue("enableoplocks"); if (val == null) { oplocktextbox.text = "not nowadays in registry"; oplocktextbox.backcolor = color.yellow; } else if (val == "1") { opslocktextbox.text = "no"; opslocktextbox.backcolor = color.red; } else { oplocktextbox.text = "yes"; oplocktextbox.backcolor = color.green; } } else { messagebox.show(""); }
as far can tell, enableoplocks
value registry key dword
value, give int
when utilize getvalue()
retrieve it. trying cast int
string
produce invalidcastexception
.
instead, should seek this:
int? val = key.getvalue("enableoplocks") int?; if(val == null) { // .. } else if(val == 1) { // ... }
or this:
object val = key.getvalue("enableoplocks"); if (val == null) { // ... } else { string strval = val.tostring(); if (strval == "1") { // ... } }
in general, please remember provide all of error info have. saying "it crashes" not informative.
c#
Comments
Post a Comment