How to check if a registry value exists using C#?

后端 未结 7 1375
庸人自扰
庸人自扰 2020-12-08 13:28

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()
{
    Registr         


        
7条回答
  •  有刺的猬
    2020-12-08 13:50

    public static bool RegistryValueExists(string hive_HKLM_or_HKCU, string registryRoot, string valueName)
    {
        RegistryKey root;
        switch (hive_HKLM_or_HKCU.ToUpper())
        {
            case "HKLM":
                root = Registry.LocalMachine.OpenSubKey(registryRoot, false);
                break;
            case "HKCU":
                root = Registry.CurrentUser.OpenSubKey(registryRoot, false);
                break;
            default:
                throw new System.InvalidOperationException("parameter registryRoot must be either \"HKLM\" or \"HKCU\"");
        }
    
        return root.GetValue(valueName) != null;
    }
    

提交回复
热议问题