如何使用C#检查注册表值是否存在? [英] How to check if a registry value exists using C#?

查看:185
本文介绍了如何使用C#检查注册表值是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过C#代码检查注册表值是否存在?
这是我的代码,我想检查'Start'是否存在。

How to check if a registry value exists by C# code? This is my code, I want to check if 'Start' exists.

public static bool checkMachineType()
{
    RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true);
    string currentKey= winLogonKey.GetValue("Start").ToString();

    if (currentKey == "0")
        return (false);
    return (true);
}


推荐答案

对于注册表项,您可以检查如果获取后为null。

For Registry Key you can check if it is null after getting it. It will be, if it doesn't exist.

对于注册表值,您可以获取当前键的Values名称,并检查此数组是否包含所需的Value名称。

For Registry Value you can get names of Values for the current key and check if this array contains the needed Value name.

示例:

public static bool checkMachineType()
{    
    RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true);
    return (winLogonKey.GetValueNames().Contains("Start"));
}

这篇关于如何使用C#检查注册表值是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆