示例#1
0
        /// <summary>
        /// Writes a value to the registry.
        /// </summary>
        /// <param name="baseKey">HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CONFIG</param>
        /// <param name="subKey">Full path of subkey. For example: SOFTWARE\CompanyName\InternalApps\WinAppComponents\Program01</param>
        /// <param name="valueName">Name of the value. For example: ConnectionString</param>
        /// <param name="value">Value to set for the value name.</param>
        /// <param name="valueKind">Specifies type of value to write. (e.g. binary, string, expandstring, multistring, dword, Qword) See <see cref="RegistryValueKind"/> for more information. </param>
        /// <returns>pfRegistryValue struct.</returns>
        /// <remarks>Throws an exception if an error is encountered during processing.</remarks>
        public static pfRegistryValue SetRegistryValue(string baseKey, string subKey, string valueName, object value, RegistryValueKind valueKind)
        {
            pfRegistryValue regValue = new pfRegistryValue();

            try
            {
                VerifySetValueParameters(baseKey, subKey, valueName, value);

                //Registry.SetValue(@"HKEY_CURRENT_USER\Software\Yahoo\Common", "Sandy", "NEW");
                RegistryHive?rh       = GetRegistryHive(baseKey);
                RegistryKey  rk       = RegistryKey.OpenBaseKey((RegistryHive)rh, RegistryView.Registry32);
                RegistryKey  rkSubKey = rk.OpenSubKey(subKey, true);
                rkSubKey.SetValue(valueName, value, valueKind);
                Object            updatedValue = rkSubKey.GetValue(valueName, "Unable to find value");
                RegistryValueKind rvk          = rkSubKey.GetValueKind(valueName);
                regValue.regValueName = valueName;
                regValue.regValue     = updatedValue;
                regValue.regValueKind = rvk;
            }

            catch (System.IO.IOException ioex)
            {
                _msg.Length = 0;
                _msg.Append("Unable to set registry value: ");
                _msg.Append(BuildKeyName(baseKey, subKey));
                _msg.Append(": ");
                _msg.Append(valueName);
                _msg.Append(". Error message: ");
                _msg.Append(AppMessages.FormatErrorMessage(ioex));
                throw new System.Exception(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error while trying to set registry value: ");
                _msg.Append(BuildKeyName(baseKey, subKey));
                _msg.Append(": ");
                _msg.Append(valueName);
                _msg.Append(". Error message: ");
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }


            return(regValue);
        }
示例#2
0
        /// <summary>
        /// Retrieve a registry value as a pfRegistryValue struct.
        /// </summary>
        /// <param name="baseKey">HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CONFIG</param>
        /// <param name="subKey">Full path of subkey. For example: SOFTWARE\CompanyName\InternalApps\WinAppComponents\Program01</param>
        /// <param name="valueName">Name of the value. For example: ConnectionString</param>
        /// <param name="defaultValue">Value if registry path not found.</param>
        /// <returns>pfRegistryValue struct representing value.</returns>
        /// <remarks>Throws an exception if an error is encountered during processing.</remarks>
        public static pfRegistryValue GetRegistryValue(string baseKey, string subKey, string valueName, object defaultValue)
        {
            pfRegistryValue regValue = new pfRegistryValue();

            try
            {
                //verify routine will throw exceptions if any problems found with the parameter values
                VerifyGetValueParameters(baseKey, subKey, valueName, defaultValue);

                //return Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Yahoo\Common", "Sandy", "defaultOLD"));
                RegistryHive?     rh       = GetRegistryHive(baseKey);
                RegistryKey       rk       = RegistryKey.OpenBaseKey((RegistryHive)rh, RegistryView.Registry32);
                RegistryKey       rkSubKey = rk.OpenSubKey(subKey, false);
                Object            value    = rkSubKey.GetValue(valueName, defaultValue);
                RegistryValueKind rvk      = rkSubKey.GetValueKind(valueName);

                regValue.regValueName = valueName;
                regValue.regValue     = value;
                regValue.regValueKind = rvk;
            }
            catch (System.IO.IOException ioex)
            {
                _msg.Length = 0;
                _msg.Append("Unable to find registry value: ");
                _msg.Append(BuildKeyName(baseKey, subKey));
                _msg.Append(": ");
                _msg.Append(valueName);
                _msg.Append(". Error message: ");
                _msg.Append(AppMessages.FormatErrorMessage(ioex));
                throw new System.Exception(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error while trying to retrieve registry value: ");
                _msg.Append(BuildKeyName(baseKey, subKey));
                _msg.Append(": ");
                _msg.Append(valueName);
                _msg.Append(". Error message: ");
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }


            return(regValue);
        }
示例#3
0
        /// <summary>
        /// Retrieve a registry value as a long.
        /// </summary>
        /// <param name="baseKey">HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CONFIG</param>
        /// <param name="subKey">Full path of subkey. For example: SOFTWARE\CompanyName\InternalApps\WinAppComponents\Program01</param>
        /// <param name="valueName">Name of the value. For example: ConnectionString</param>
        /// <param name="defaultValue">Value if registry path not found.</param>
        /// <returns>Long representing value.</returns>
        /// <remarks>Throws an exception if an error is encountered during processing.</remarks>
        public static long GetRegistryValueLong(string baseKey, string subKey, string valueName, object defaultValue)
        {
            pfRegistryValue rv = GetRegistryValue(baseKey, subKey, valueName, defaultValue);

            if (rv.regValueKind != RegistryValueKind.DWord && rv.regValueKind != RegistryValueKind.QWord)
            {
                _msg.Length = 0;
                _msg.Append("Value for ");
                _msg.Append(valueName);
                _msg.Append(" is not a long: ");
                _msg.Append(rv.regValue.ToString());
                throw new System.Exception(_msg.ToString());
            }
            return((long)rv.regValue);
        }
示例#4
0
        /// <summary>
        /// Retrieve a registry value as an object.
        /// </summary>
        /// <param name="baseKey">HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CONFIG</param>
        /// <param name="subKey">Full path of subkey. For example: SOFTWARE\CompanyName\InternalApps\WinAppComponents\Program01</param>
        /// <param name="valueName">Name of the value. For example: ConnectionString</param>
        /// <param name="defaultValue">Value if registry path not found.</param>
        /// <returns>Object representing value.</returns>
        /// <remarks>Throws an exception if an error is encountered during processing.</remarks>
        public static object GetRegistryValueObject(string baseKey, string subKey, string valueName, object defaultValue)
        {
            pfRegistryValue rv = GetRegistryValue(baseKey, subKey, valueName, defaultValue);

            return((object)rv.regValue);
        }
示例#5
0
        /// <summary>
        /// Retrieve a registry value as a byte array.
        /// </summary>
        /// <param name="baseKey">HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CONFIG</param>
        /// <param name="subKey">Full path of subkey. For example: SOFTWARE\CompanyName\InternalApps\WinAppComponents\Program01</param>
        /// <param name="valueName">Name of the value. For example: ConnectionString</param>
        /// <param name="defaultValue">Value if registry path not found.</param>
        /// <returns>Byte array representing value.</returns>
        /// <remarks>Throws an exception if an error is encountered during processing.</remarks>
        public static byte[] GetRegistryValueBinary(string baseKey, string subKey, string valueName, object defaultValue)
        {
            pfRegistryValue rv = GetRegistryValue(baseKey, subKey, valueName, defaultValue);

            return((byte[])rv.regValue);
        }