使用set和get属性读取注册表

本文关键字:读取 注册表 属性 get set 使用 | 更新日期: 2023-09-27 17:50:35

我不知道我做错了什么。我有一个类创建获得一些reg键值,我不断获得类型转换错误。我传入钥匙的名字。键的值是int型。它也应该是返回值。

我一直得到这个错误

不能隐式地将类型'string'转换为int

在以下位置:

rValue = (string)myKey.GetValue("DebugLog");
&set { DebugLog = getDebugLog(value);}

在c#

中注意
private int DebugLog;
private int getDebugLog(String name)
{
    int rValue;
    myKey = Registry.LocalMachine.OpenSubKey(regKey + name, false);
    rValue = (string)myKey.GetValue("DebugLog");
    return rValue;
}
public int debugLog
{
    get { return DebugLog; }
    set { DebugLog = getDebugLog(value);}
}

使用set和get属性读取注册表

注册表在这里是无关紧要的-您将值转换为string,然后尝试将该表达式分配给int变量。就像这样:

object x = "hello";
int y = (string) x;

那是不可能的。如果您想将字符串转换为整数,则需要对其进行解析,例如使用int.Parseint.TryParse

如果注册表中的值是,实际上是一个整数,只需将其转换为该值即可。

rValue = Convert.ToInt32(myKey.GetValue("DebugLog"));

声明rValue为整型,但将其设置为字符串。试试这个:

rvalue = Int32.Parse((string)myKey.GetValue("DebugLog"));