Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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#仅当注册表值名称已存在时才设置新注册表值_C#_Registry_Setvalue - Fatal编程技术网

c#仅当注册表值名称已存在时才设置新注册表值

c#仅当注册表值名称已存在时才设置新注册表值,c#,registry,setvalue,C#,Registry,Setvalue,我的问题是,当我使用Registry.SetValue时,我只希望它更新现有值。如果输入的值名称不存在,我不想创建它。我有用户输入的变量数据,因此无法在代码中硬编码路径 我的套装代码 public class SetRegistryValue : CodeActivity { [RequiredArgument] public InArgument<string> kpath { get; set; } public InArgument<st

我的问题是,当我使用Registry.SetValue时,我只希望它更新现有值。如果输入的值名称不存在,我不想创建它。我有用户输入的变量数据,因此无法在代码中硬编码路径

我的套装代码

    public class SetRegistryValue : CodeActivity
{
    [RequiredArgument]
    public InArgument<string> kpath { get; set; }

    public InArgument<string> txtName { get; set; }

    [RequiredArgument]
    public InArgument<string> kvaluename { get; set; }

    [RequiredArgument]
    public InArgument<string> kvalue { get; set; }

    //This will set the value of an key that is defined by user
    protected override void Execute(CodeActivityContext context)
    {

        string KeyPath = this.kpath.Get(context);
        string KeyValueName = this.kvaluename.Get(context);
        string KeyValue = this.kvalue.Get(context);
        string KeyName = Path.GetDirectoryName(KeyPath);
        string KeyFileName = Path.GetFileName(KeyPath);
        string fullKeyPath = KeyName + "\\" +  KeyFileName;

        Registry.SetValue(fullKeyPath, KeyValueName, KeyValue, RegistryValueKind.String);
    }
}
public类SetRegistryValue:CodeActivity
{
[必需参数]
公共InArgument kpath{get;set;}
公共InArgument txtName{get;set;}
[必需参数]
公共InArgument kvaluename{get;set;}
[必需参数]
公共InArgument kvalue{get;set;}
//这将设置由用户定义的键的值
受保护的覆盖无效执行(CodeActivityContext上下文)
{
字符串KeyPath=this.kpath.Get(上下文);
string KeyValueName=this.kvaluename.Get(上下文);
string KeyValue=this.kvalue.Get(上下文);
字符串KeyName=Path.GetDirectoryName(KeyPath);
字符串KeyFileName=Path.GetFileName(KeyPath);
字符串fullKeyPath=KeyName+“\\”+KeyFileName;
SetValue(fullKeyPath、KeyValueName、KeyValue、RegistryValueKind.String);
}
}
使用以下方法:

检索指定注册表项中与指定名称关联的值。如果在指定的键中找不到该名称,则返回您提供的默认值,如果指定的键不存在,则返回null

如果要测试
keyName
是否存在,请测试null:

var myValue
  = Registry.GetValue(@"HKEY_CURRENT_USER\missing_key", "missing_value", "hi");

// myValue = null (because that's just what GetValue returns)
如果要测试
valueName
是否存在,请测试默认值:

var myValue
  = Registry.GetValue(@"HKEY_CURRENT_USER\valid_key", "missing_value", null);

// myValue = null (because that's what you specified as the defaultValue)

如果路径可能无效,可以尝试使用
try/catch
块将其包围:

try
{
    var myValue = Registry.GetValue( ... );  // throws exception on invalid keyName

    if (myValue != null)
        Registry.SetValue( ... );
}
catch (ArgumentException ex)
{
    // do something like tell user that path is invalid
}

当然我们会帮你的,你能解释清楚你想要什么吗。如果可能的话,请提供一些例子。@sudhakar,比如grant说的bellow,我想得到值名的值。我想查看注册表中的值名称,如果有,请将该值更改为活动中提供的任何新值。但是如果值名不在路径中,请不要执行setvalue。Think是我最初认为要使用的,但我无法在代码中控制getvalue的路径,这是我出错时使用的。您可以从上面的代码中看到我的set活动。我的get活动看起来很相似,但显然是一个get活动。看起来这样就可以了。将在周一进行全面测试,但如果你不从我这里回来,它起作用了。看起来会的,我试过类似的方法,但并没有宣布为var…我犯了一个大错误。p、 美国也注意到你来自克利夫兰…去布朗