Tony Campbell
Tony Campbell

Reputation: 63

getting a registry key and value in C#

Sorry if this is simple, I haven't coded since college. I'm trying to write a program to view registry entries in Windows 7. I want to check to see if the registry value exists first, then check to see what the value is. If it doesn't exist, I want one message, if it does exist, I want one message reflecting a value of 1, and another reflecting a value of 0. I got the code to work if the registry key doesn't exist, but if I add the key and value it crashes. Not sure what I'm doing wrong here. Any suggestions would be appreciated. Here is my 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 Present 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("");
}

Upvotes: 1

Views: 5520

Answers (3)

JLRishe
JLRishe

Reputation: 101680

As far as I can tell, the EnableOplocks value for that registry key is a DWORD value, which will give you an int when you use GetValue() to retrieve it. Trying to cast an int to a string will produce an InvalidCastException.

Instead, you should try this:

int? val = Key.GetValue("EnableOplocks") as 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 to provide all of the error information you have. Saying "it crashes" is not very informative.

Upvotes: 4

New Developer
New Developer

Reputation: 3305

Use follow;

string val = Key.GetValue("EnableOplocks").ToString();  

EDIT

using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\LanmanServer\Parameters"))
if (Key != null)
{
    var val = Key.GetValue("EnableOplocks");
    if (val == null)
    {
        oplockTextBox.Text = "Not Present In Registry";
        oplockTextBox.BackColor = Color.Yellow;
    }
    else if (val.ToString() == "1")
    {
        opslockTextBox.Text = "NO";
        opslockTextBox.BackColor = Color.Red;
    }
    else
    {
        oplockTextBox.Text = "YES";
        oplockTextBox.BackColor = Color.Green;
    }
}
else
{
    MessageBox.Show("");
}

Upvotes: 1

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

The registry can hold data-types other than string. What is happening is you are likely getting a int returned and that is why you are crashing when you attempt to cast a int to a string

Get the value back and store it in a object and have your debugger break. You should then be able to see what datatype is stored in the object and change your code to make the correct cast.

The other option would be use .ToString() instead of casting, you would need to compare the string 1 (like you are now) instead of the value 1. However, I always prefer to just use the correct type instead of turning everything in to strings.

Upvotes: 1

Related Questions