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

And*_*y12 0 c# registry

我不确定我在这里做错了什么.我有一个类来创建一些reg键值,我继续得到类型转换错误.我传了钥匙的名字.键的值是int.哪个也应该是返回值.

我继续得到这个错误

无法将类型'string'隐式转换为int

在以下位置:

rValue = (string)myKey.GetValue("DebugLog");
&set { DebugLog = getDebugLog(value);}
Run Code Online (Sandbox Code Playgroud)

请注意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);}
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 7

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

object x = "hello";
int y = (string) x;
Run Code Online (Sandbox Code Playgroud)

那永远不会奏效.如果要将字符串转换为整数,则需要解析它,例如使用int.Parseint.TryParse.

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