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

后端 未结 7 1387
庸人自扰
庸人自扰 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:32
    string keyName=@"HKEY_LOCAL_MACHINE\System\CurrentControlSet\services\pcmcia";
    string valueName="Start";
    if (Registry.GetValue(keyName, valueName, null) == null)
    {
         //code if key Not Exist
    }
    else
    {
         //code if key Exist
    }
    
    0 讨论(0)
  • 2020-12-08 13:41
    public bool ValueExists(RegistryKey Key, string Value)
    {
       try
       {
           return Key.GetValue(Value) != null;
       }
       catch
       {
           return false;
       }
    }
    

    This simple function will return true only if a value is found but it is not null, else will return false if the value exists but it is null or the value doesn't exists in the key.


    USAGE for your question:

    if (ValueExists(winLogonKey, "Start")
    {
        // The values exists
    }
    else
    {
        // The values does not exists
    }
    
    0 讨论(0)
  • 2020-12-08 13:43
            internal static Func<string, string, bool> regKey = delegate (string KeyLocation, string Value)
            {
                // get registry key with Microsoft.Win32.Registrys
                RegistryKey rk = (RegistryKey)Registry.GetValue(KeyLocation, Value, null); // KeyLocation and Value variables from method, null object because no default value is present. Must be casted to RegistryKey because method returns object.
                if ((rk) == null) // if the RegistryKey is null which means it does not exist
                {
                    // the key does not exist
                    return false; // return false because it does not exist
                }
                // the registry key does exist
                return true; // return true because it does exist
            };
    

    usage:

            // usage:
            /* Create Key - while (loading)
            {
                RegistryKey k;
                k = Registry.CurrentUser.CreateSubKey("stuff");
                k.SetValue("value", "value");
                Thread.Sleep(int.MaxValue);
            }; // no need to k.close because exiting control */
    
    
            if (regKey(@"HKEY_CURRENT_USER\stuff  ...  ", "value"))
            {
                 // key exists
                 return;
            }
            // key does not exist
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-08 13:52

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

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

    Example:

    public static bool checkMachineType()
    {    
        RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true);
        return (winLogonKey.GetValueNames().Contains("Start"));
    }
    
    0 讨论(0)
  • 2020-12-08 13:53

    Of course, "Fagner Antunes Dornelles" is correct in its answer. But it seems to me that it is worth checking the registry branch itself in addition, or be sure of the part that is exactly there.

    For example ("dirty hack"), i need to establish trust in the RMS infrastructure, otherwise when i open Word or Excel documents, i will be prompted for "Active Directory Rights Management Services". Here's how i can add remote trust to me servers in the enterprise infrastructure.

    foreach (var strServer in listServer)
    {
        try
        {
            RegistryKey regCurrentUser = Registry.CurrentUser.OpenSubKey($"Software\\Classes\\Local Settings\\Software\\Microsoft\\MSIPC\\{strServer}", false);
            if (regCurrentUser == null)
                throw new ApplicationException("Not found registry SubKey ...");
            if (regCurrentUser.GetValueNames().Contains("UserConsent") == false)
                throw new ApplicationException("Not found value in SubKey ...");
        }
        catch (ApplicationException appEx)
        {
            Console.WriteLine(appEx);
            try
            {
                RegistryKey regCurrentUser = Registry.CurrentUser.OpenSubKey($"Software\\Classes\\Local Settings\\Software\\Microsoft\\MSIPC", true);
                RegistryKey newKey = regCurrentUser.CreateSubKey(strServer, true);
                newKey.SetValue("UserConsent", 1, RegistryValueKind.DWord);
            }
            catch(Exception ex)
            {
                Console.WriteLine($"{ex} Pipec kakoito ...");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题