Beispiel #1
0
 private bool CopyRegistryKey(IRegistryWrapper key, string path, string destination, bool recurse, bool streamResult, bool streamFirstOnly)
 {
     bool flag = true;
     if (recurse && this.ErrorIfDestinationIsSourceOrChildOfSource(path, destination))
     {
         return false;
     }
     tracer.WriteLine("destination = {0}", new object[] { destination });
     IRegistryWrapper regkeyForPath = this.GetRegkeyForPath(destination, true);
     string childName = this.GetChildName(path);
     string parentPath = destination;
     if (regkeyForPath == null)
     {
         parentPath = this.GetParentPath(destination, null);
         childName = this.GetChildName(destination);
         regkeyForPath = this.GetRegkeyForPathWriteIfError(parentPath, true);
     }
     if (regkeyForPath == null)
     {
         return false;
     }
     string str3 = this.MakePath(parentPath, childName);
     string copyKeyAction = RegistryProviderStrings.CopyKeyAction;
     string copyKeyResourceTemplate = RegistryProviderStrings.CopyKeyResourceTemplate;
     string target = string.Format(base.Host.CurrentCulture, copyKeyResourceTemplate, new object[] { path, destination });
     if (base.ShouldProcess(target, copyKeyAction))
     {
         IRegistryWrapper wrapper2 = null;
         try
         {
             wrapper2 = regkeyForPath.CreateSubKey(childName);
         }
         catch (NotSupportedException exception)
         {
             base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.InvalidOperation, childName));
         }
         if (wrapper2 != null)
         {
             string[] valueNames = key.GetValueNames();
             for (int i = 0; i < valueNames.Length; i++)
             {
                 if (base.Stopping)
                 {
                     regkeyForPath.Close();
                     wrapper2.Close();
                     return false;
                 }
                 wrapper2.SetValue(valueNames[i], key.GetValue(valueNames[i], null, RegistryValueOptions.DoNotExpandEnvironmentNames), key.GetValueKind(valueNames[i]));
             }
             if (streamResult)
             {
                 this.WriteRegistryItemObject(wrapper2, str3);
                 if (streamFirstOnly)
                 {
                     streamResult = false;
                 }
             }
         }
     }
     regkeyForPath.Close();
     if (recurse)
     {
         string[] subKeyNames = key.GetSubKeyNames();
         for (int j = 0; j < subKeyNames.Length; j++)
         {
             if (base.Stopping)
             {
                 return false;
             }
             string str7 = this.MakePath(path, subKeyNames[j]);
             string str8 = this.MakePath(str3, subKeyNames[j]);
             IRegistryWrapper wrapper3 = this.GetRegkeyForPath(str7, false);
             bool flag2 = this.CopyRegistryKey(wrapper3, str7, str8, recurse, streamResult, streamFirstOnly);
             wrapper3.Close();
             if (!flag2)
             {
                 flag = flag2;
             }
         }
     }
     return flag;
 }
Beispiel #2
0
        } // CopyItem


        private bool CopyRegistryKey(
            IRegistryWrapper key,
            string path,
            string destination,
            bool recurse,
            bool streamResult,
            bool streamFirstOnly)
        {
            bool result = true;

            // Make sure we are not trying to do a recursive copy of a key
            // to itself or a child of itself.

            if (recurse)
            {
                if (ErrorIfDestinationIsSourceOrChildOfSource(path, destination))
                {
                    return false;
                }
            }

            Dbg.Diagnostics.Assert(
                key != null,
                "The key should have been validated by the caller");

            Dbg.Diagnostics.Assert(
                !String.IsNullOrEmpty(path),
                "The path should have been validated by the caller");

            Dbg.Diagnostics.Assert(
                !String.IsNullOrEmpty(destination),
                "The destination should have been validated by the caller");

            s_tracer.WriteLine("destination = {0}", destination);

            // Get the parent key of the destination
            // If the destination already exists and is a key, then it becomes
            // the container of the source. If the key doesn't already exist
            // the parent of the destination path becomes the container of source.

            IRegistryWrapper newParentKey = GetRegkeyForPath(destination, true);
            string destinationName = GetChildName(path);
            string destinationParent = destination;

            if (newParentKey == null)
            {
                destinationParent = GetParentPath(destination, null);
                destinationName = GetChildName(destination);

                newParentKey = GetRegkeyForPathWriteIfError(destinationParent, true);
            }

            if (newParentKey == null)
            {
                // The key was not found.
                // An error should have been written by GetRegkeyForPathWriteIfError
                return false;
            }

            string destinationPath = MakePath(destinationParent, destinationName);


            // Confirm the copy item with the user

            string action = RegistryProviderStrings.CopyKeyAction;

            string resourceTemplate = RegistryProviderStrings.CopyKeyResourceTemplate;

            string resource =
                    String.Format(
                        Host.CurrentCulture,
                        resourceTemplate,
                        path,
                        destination);

            if (ShouldProcess(resource, action))
            {
                // Create new key under the parent

                IRegistryWrapper newKey = null;
                try
                {
                    newKey = newParentKey.CreateSubKey(destinationName);
                }
                catch (NotSupportedException e)
                {
                    WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.InvalidOperation, destinationName));
                }

                if (newKey != null)
                {
                    // Now copy all the properties from the source to the destination

                    string[] valueNames = key.GetValueNames();

                    for (int index = 0; index < valueNames.Length; ++index)
                    {
                        // Making sure to obey the StopProcessing.
                        if (Stopping)
                        {
                            newParentKey.Close();
                            newKey.Close();
                            return false;
                        }

                        newKey.SetValue(
                            valueNames[index],
                            key.GetValue(valueNames[index], null, RegistryValueOptions.DoNotExpandEnvironmentNames),
                            key.GetValueKind(valueNames[index]));
                    }

                    if (streamResult)
                    {
                        // Write out the key that was copied

                        WriteRegistryItemObject(newKey, destinationPath);

                        if (streamFirstOnly)
                        {
                            streamResult = false;
                        }
                    }
                }
            }

            newParentKey.Close();

            if (recurse)
            {
                // Copy all the subkeys

                string[] subkeyNames = key.GetSubKeyNames();

                for (int keyIndex = 0; keyIndex < subkeyNames.Length; ++keyIndex)
                {
                    // Making sure to obey the StopProcessing.
                    if (Stopping)
                    {
                        return false;
                    }

                    // Make the new path under the copy path.

                    string subKeyPath = MakePath(path, subkeyNames[keyIndex]);
                    string newSubKeyPath = MakePath(destinationPath, subkeyNames[keyIndex]);

                    IRegistryWrapper childKey = GetRegkeyForPath(subKeyPath, false);

                    bool subtreeResult = CopyRegistryKey(childKey, subKeyPath, newSubKeyPath, recurse, streamResult, streamFirstOnly);

                    childKey.Close();

                    if (!subtreeResult)
                    {
                        result = subtreeResult;
                    }
                }
            }

            return result;
        } // CopyRegistryKey