从方法参数设置 RegistryValueKind

Set RegistryValueKind from a method param

我正在尝试使用方法中的参数设置 RegistryValueKind,但这不起作用,看起来无法通过参数进行设置。有办法解决这个问题吗?

static bool TrySetKey(string Dir, string Value, string Data, string ValueKind)
{
    if (!CanReadKey(Dir))
        return false;
    using (RegistryKey RegKey = Registry.LocalMachine.OpenSubKey(Dir, true))
        if (RegKey == null)
            return false;
        RegKey.SetValue(Value, Data, RegistryValueKind.ValueKind);
        return true;
    }
}

解决方案:我应该提到我将 Data 的字符串参数更改为对象,这样我就可以传递不同的值。

static bool TrySetKey(string Value, Object Data, RegistryValueKind ValueKind)
{
   // code here 
     RegKey.SetValue(Value, Data, ValueKind);
   return true;
}

TrySetKey("Value Name", "1", RegistryValueKind.DWord)

RegistryValueKind 是一个枚举,所以你不能从字符串中获取它的值,最好像下面这样更改你的方法签名:

static bool TrySetKey(string Dir, string Value, string Data, RegistryValueKind ValueKind)
{
   // code here 
   // and set value like this
     RegKey.SetValue(Value, Data, ValueKind);
   return true;
}

这样你就可以像这样调用这个方法:

TrySetKey("Value Name", "1", RegistryValueKind.DWord)