tools/VsIdeHostAdapter/TargetAddIn/Common/RegistryHelper.cs
/// <summary>
/// Safely attempt to read the value of the specified key in registry.
/// If the key does not exist or not enough security permissions returns false
/// 'ignoring exception' means it should not throw except really bad exceptions like outofmery, clrexecution, etc.
/// </summary>
/// <param name="hive">Registry hive, like: Registry.LocalMachine.</param>
/// <param name="subkeyName">The name of the subkey under hive specified by the hive parameter.</param>
/// <param name="valueName">The name of the value. To get default key value, specify null.</param>
/// <param name="value">The output variable that will contain the read value if successful.</param>
Safely attempt to read the value of the specified key in registry.
If the key does not exist or not enough security permissions returns false
'ignoring exception' means it should not throw except really bad exceptions like outofmery, clrexecution, etc.
Safely attempt to read the value of the specified key in registry . If the key does not exist or not enough security permissions returns false ' ignoring exception ' means it should not throw except really bad exceptions like outofmery clrexecution etc .
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        internal static bool TryGetValueIgnoringExceptions(RegistryKey hive, string subkeyName, string valueName, out T value)
        {
            Debug.Assert(hive != null, "GetValueIgnoringExceptions: hive is null.");
            Debug.Assert(!string.IsNullOrEmpty(subkeyName), "GetValueIgnoringExceptions: subkeyName is null.");
            try
            {
                using (RegistryKey key = hive.OpenSubKey(subkeyName))
                {
                    if (key != null)
                    {
                        object tmpValue = key.GetValue(valueName);
                        if (tmpValue != null)
                        {
                            value = (T)tmpValue;
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Fail(string.Format(CultureInfo.InvariantCulture,
                    "RegistryHelper.GetValueIgnoringExceptions: {0}", ex));
            }
            value = default(T);
            return false;
        }
[ SuppressMessage ( " " , " " ) ] internal static bool TryGetValueIgnoringExceptions ( RegistryKey hive , string subkeyName , string valueName , out T value ) { Debug . Assert ( hive != null , " " ) ; Debug . Assert ( ! string . IsNullOrEmpty ( subkeyName ) , " " ) ; try { using ( RegistryKey key = hive . OpenSubKey ( subkeyName ) ) { if ( key != null ) { object tmpValue = key . GetValue ( valueName ) ; if ( tmpValue != null ) { value = ( T ) tmpValue ; return true ; } } } } catch ( Exception ex ) { Debug . Fail ( string . Format ( CultureInfo . InvariantCulture , " " , ex ) ) ; } value = default ( T ) ; return false ; }
Safely attempt to read the value of the specified key in registry.
Safely attempt to read the value of the specified key in registry .
// valueName can be null or empty - this is to get default key value. // Too many: ArgumentException, ObjectDisposedException, SecurityException, InvalidCastException, UnauthorizedAccessException // Ignore the exception.
hive
RegistryKey
Registry hive, like: Registry.LocalMachine.
Registry hive like : Registry . LocalMachine .
subkeyName
string
The name of the subkey under hive specified by the hive parameter.
The name of the subkey under hive specified by the hive parameter .
valueName
string
The name of the value. To get default key value, specify null.
The name of the value . To get default key value specify null .
T
out
value
The output variable that will contain the read value if successful.
The output variable that will contain the read value if successful .