Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用set和get属性读取注册表_C#_Registry - Fatal编程技术网

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

C# 使用set和get属性读取注册表,c#,registry,C#,Registry,我不确定我在这里做错了什么。我创建了一个类来获取一些注册表键值,并且不断地获取类型转换错误。我把钥匙的名字传进去。键的值是一个int。它也应该是返回值 我不断地犯这个错误 无法将类型“string”隐式转换为int 在下列地点: rValue = (string)myKey.GetValue("DebugLog"); &set { DebugLog = getDebugLog(value);} 请在c中注意这一点 private int DebugLog; private int g

我不确定我在这里做错了什么。我创建了一个类来获取一些注册表键值,并且不断地获取类型转换错误。我把钥匙的名字传进去。键的值是一个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);}
}

注册表在这里是不相关的-您正在将一个值强制转换为字符串,然后尝试将该表达式分配给一个int变量。是这样的:

object x = "hello";
int y = (string) x;
那是行不通的。如果要将字符串转换为整数,则需要对其进行解析,例如使用或


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

您将右值声明为int,但将其设置为字符串。试试这个:

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

什么是调试日志?是否设置了值

private int getDebugLog(String name)
{
  int rValue;
  myKey = Registry.LocalMachine.OpenSubKey(regKey + name, false);
  object o = myKey.GetValue("DebugLog");
  if (o != null) {
    rValue = o.ToString();
    if (!String.IsNullOrEmpty(rValue)) {
      return Convert.ToInt32(Convert.ToDecimal(rValue));
    }
  }
  return null;
}

ok rvalue=Int32.ParsestringmyKey.GetValueDebugLog;工作正常,现在我在get和set方法中得到了无法从int转换为string的结果。public int debugLog{get{return debugLog;}set{DebugLogAutoRoll=getDebugLogvalue;}}您需要使用myKey.GetValueDebugLog,value.ToString编写一个set函数。如果愿意,请接受此答案。int函数不能返回null。
private int getDebugLog(String name)
{
  int rValue;
  myKey = Registry.LocalMachine.OpenSubKey(regKey + name, false);
  object o = myKey.GetValue("DebugLog");
  if (o != null) {
    rValue = o.ToString();
    if (!String.IsNullOrEmpty(rValue)) {
      return Convert.ToInt32(Convert.ToDecimal(rValue));
    }
  }
  return null;
}