简体   繁体   English

在C#中获取注册表项和值

[英]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. 我试图编写一个程序来查看Windows 7中的注册表项。我想先检查注册表值是否存在,然后再检查该值是什么。 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. 如果它不存在,我想要一条消息,如果它确实存在,我希望一条消息反映的值为1,而另一条消息反映的值为0。如果注册表项不存在,则使代码起作用,但是,如果我添加键和值,它将崩溃。 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("");
}

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. 据我所知,该注册表项的EnableOplocks值是DWORD值,当您使用GetValue()检索它时,它将为您提供一个int Trying to cast an int to a string will produce an InvalidCastException . 尝试将int InvalidCastExceptionstring将产生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. 说“它崩溃”不是很有帮助。

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 发生的事情是您很可能会返回一个int ,这就是为什么在尝试将int转换为string时崩溃的原因

Get the value back and store it in a object and have your debugger break. 取回该值并将其存储在一个object并使调试器中断。 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. 另一个选择是使用.ToString()而不是强制转换,您将需要比较字符串1(就像现在一样)而不是值1。但是,我总是更喜欢只使用正确的类型而不是将所有内容都变成到字符串。

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("");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM