Esempio n. 1
0
 private void CopyProperty(IRegistryWrapper sourceKey, IRegistryWrapper destinationKey, string sourceProperty, string destinationProperty, bool writeOnSuccess)
 {
     string propertyName = this.GetPropertyName(sourceProperty);
     this.GetPropertyName(destinationProperty);
     object obj2 = sourceKey.GetValue(sourceProperty);
     RegistryValueKind valueKind = sourceKey.GetValueKind(sourceProperty);
     destinationKey.SetValue(destinationProperty, obj2, valueKind);
     if (writeOnSuccess)
     {
         this.WriteWrappedPropertyObject(obj2, propertyName, sourceKey.Name);
     }
 }
Esempio n. 2
0
 private void SetRegistryValue(IRegistryWrapper key, string propertyName, object value, RegistryValueKind kind, string path, bool writeResult)
 {
     string valueName = this.GetPropertyName(propertyName);
     RegistryValueKind unknown = RegistryValueKind.Unknown;
     if (kind == RegistryValueKind.Unknown)
     {
         unknown = GetValueKindForProperty(key, valueName);
     }
     if (unknown != RegistryValueKind.Unknown)
     {
         try
         {
             value = ConvertValueToKind(value, unknown);
             kind = unknown;
         }
         catch (InvalidCastException)
         {
             unknown = RegistryValueKind.Unknown;
         }
     }
     if (unknown == RegistryValueKind.Unknown)
     {
         if (kind == RegistryValueKind.Unknown)
         {
             if (value != null)
             {
                 kind = GetValueKindFromObject(value);
             }
             else
             {
                 kind = RegistryValueKind.String;
             }
         }
         value = ConvertValueToKind(value, kind);
     }
     key.SetValue(valueName, value, kind);
     if (writeResult)
     {
         object obj2 = key.GetValue(valueName);
         this.WriteWrappedPropertyObject(obj2, propertyName, path);
     }
 }
Esempio n. 3
0
        private object ResetRegistryKeyValue(IRegistryWrapper key, string valueName)
        {
            RegistryValueKind valueKind = key.GetValueKind(valueName);
            object obj2 = null;
            switch (valueKind)
            {
                case RegistryValueKind.Unknown:
                case RegistryValueKind.Binary:
                    obj2 = new byte[0];
                    break;

                case RegistryValueKind.String:
                case RegistryValueKind.ExpandString:
                    obj2 = "";
                    break;

                case RegistryValueKind.DWord:
                    obj2 = 0;
                    break;

                case RegistryValueKind.MultiString:
                    obj2 = new string[0];
                    break;

                case RegistryValueKind.QWord:
                    obj2 = 0L;
                    break;
            }
            try
            {
                key.SetValue(valueName, obj2, valueKind);
            }
            catch (IOException exception)
            {
                base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.WriteError, valueName));
            }
            catch (SecurityException exception2)
            {
                base.WriteError(new ErrorRecord(exception2, exception2.GetType().FullName, ErrorCategory.PermissionDenied, valueName));
            }
            catch (UnauthorizedAccessException exception3)
            {
                base.WriteError(new ErrorRecord(exception3, exception3.GetType().FullName, ErrorCategory.PermissionDenied, valueName));
            }
            return obj2;
        }
Esempio n. 4
0
        /// <summary>
        /// Sets or creates a registry value on a key.
        /// </summary>
        /// 
        /// <param name="key">
        /// The key to set or create the value on.
        /// </param>
        /// 
        /// <param name="propertyName">
        /// The name of the value to set or create.
        /// </param>
        /// 
        /// <param name="value">
        /// The new data for the value.
        /// </param>
        /// 
        /// <param name="kind">
        /// The RegistryValueKind of the value.
        /// </param>
        /// 
        /// <param name="path">
        /// The path to the key that the value is being set on.
        /// </param>
        /// 
        /// <param name="writeResult">
        /// If true, the value that is set will be written out.
        /// </param>
        /// 
        private void SetRegistryValue(
            IRegistryWrapper key,
            string propertyName,
            object value,
            RegistryValueKind kind,
            string path,
            bool writeResult)
        {
            Dbg.Diagnostics.Assert(
                key != null,
                "Caller should have verified key");

            string propertyNameToSet = GetPropertyName(propertyName);

            RegistryValueKind existingKind = RegistryValueKind.Unknown;

            // If user does not specify a kind: get the valuekind if the property
            // already exists
            if (kind == RegistryValueKind.Unknown)
            {
                existingKind = GetValueKindForProperty(key, propertyNameToSet);
            }

            // try to do a conversion based on the existing kind, if we
            // were able to retrieve one
            if (existingKind != RegistryValueKind.Unknown)
            {
                try
                {
                    value = ConvertValueToKind(value, existingKind);
                    kind = existingKind;
                }
                catch (InvalidCastException)
                {
                    // failed attempt, we reset to unknown to let the
                    // default conversion process take over
                    existingKind = RegistryValueKind.Unknown;
                }
            }

            // set the kind as defined by the user
            if (existingKind == RegistryValueKind.Unknown)
            {
                // we use to kind passed in, either because we had
                // a valid one or because we failed to retrieve an existing kind to match
                if (kind == RegistryValueKind.Unknown)
                {
                    // set the kind based on value
                    if (value != null)
                    {
                        kind = GetValueKindFromObject(value);
                    }
                    else
                    {
                        // if no value and unknown kind, then default to empty string
                        kind = RegistryValueKind.String;
                    }
                }

                value = ConvertValueToKind(value, kind);
            }

            key.SetValue(propertyNameToSet, value, kind);

            if (writeResult)
            {
                // Now write out the value
                object newValue = key.GetValue(propertyNameToSet);

                WriteWrappedPropertyObject(newValue, propertyName, path);
            }
        } // SetRegistryValue
Esempio n. 5
0
        /// <summary>
        /// IT resets the a registry key value to its default
        /// </summary>
        /// <param name="key">Key whose value has to be reset</param>
        /// <param name="valueName">name of the value to reset</param>
        /// <returns>default value the key was set to</returns>
        private object ResetRegistryKeyValue(IRegistryWrapper key, string valueName)
        {
            RegistryValueKind valueKind = key.GetValueKind(valueName);
            object defaultValue = null;

            switch (valueKind)
            {
                // NOTICE: we assume that an unknown type is treated as
                // the same as a binary blob
                case RegistryValueKind.Binary:
                case RegistryValueKind.Unknown:
                    {
                        defaultValue = new byte[0];
                    }
                    break;
                case RegistryValueKind.DWord:
                    {
                        defaultValue = (int)0;
                    }
                    break;
                case RegistryValueKind.ExpandString:
                case RegistryValueKind.String:
                    {
                        defaultValue = "";
                    }
                    break;
                case RegistryValueKind.MultiString:
                    {
                        defaultValue = new string[0];
                    }
                    break;
                case RegistryValueKind.QWord:
                    {
                        defaultValue = (long)0;
                    }
                    break;
            }

            try
            {
                key.SetValue(valueName, defaultValue, valueKind);
            }
            catch (System.IO.IOException ioException)
            {
                // An exception occurred while trying to set the value. Write
                // out the error.

                WriteError(new ErrorRecord(ioException, ioException.GetType().FullName, ErrorCategory.WriteError, valueName));
            }
            catch (System.Security.SecurityException securityException)
            {
                // An exception occurred while trying to set the value. Write
                // out the error.

                WriteError(new ErrorRecord(securityException, securityException.GetType().FullName, ErrorCategory.PermissionDenied, valueName));
            }
            catch (System.UnauthorizedAccessException unauthorizedAccessException)
            {
                // An exception occurred while trying to get the key. Write
                // out the error.

                WriteError(new ErrorRecord(unauthorizedAccessException, unauthorizedAccessException.GetType().FullName, ErrorCategory.PermissionDenied, valueName));
            }
            return defaultValue;
        }
Esempio n. 6
0
        private void CopyProperty(
            IRegistryWrapper sourceKey,
            IRegistryWrapper destinationKey,
            string sourceProperty,
            string destinationProperty,
            bool writeOnSuccess)
        {
            string realSourceProperty = GetPropertyName(sourceProperty);
            string realDestinationProperty = GetPropertyName(destinationProperty);

            object sourceValue = sourceKey.GetValue(sourceProperty);
            RegistryValueKind sourceKind = sourceKey.GetValueKind(sourceProperty);

            destinationKey.SetValue(destinationProperty, sourceValue, sourceKind);

            if (writeOnSuccess)
            {
                WriteWrappedPropertyObject(sourceValue, realSourceProperty, sourceKey.Name);
            }
        } // CopyProperty