private void AddItemCopy(CopyItemContext copyItemContext, out Exception e)
        {
            e = null;
            try
            {
                switch (copyItemContext.Item.Kind)
                {
                case (Constants.vsProjectItemKindSolutionItems):
                case (Constants.vsProjectItemKindPhysicalFile):
                {
                    for (short i = 1; i <= copyItemContext.Item.FileCount; ++i)
                    {
                        var file        = copyItemContext.Item.get_FileNames(i);
                        var newFileName = copyItemContext.CopyPath;

                        if (copyItemContext.IsWithinProject)
                        {
                            copyItemContext.DestinationItems.AddFromTemplate(file, newFileName);
                        }
                        else
                        {
                            copyItemContext.DestinationItems.AddFromFileCopy(file);
                            copyItemContext.Root.LastCreatedItem.Name = newFileName;
                        }
                    }
                    break;
                }

                case (Constants.vsProjectItemKindPhysicalFolder):
                    copyItemContext.DestinationItems.AddFolder(copyItemContext.CopyPath,
                                                               Constants.vsProjectItemKindPhysicalFolder);
                    break;

                case (Constants.vsProjectItemKindVirtualFolder):
                    copyItemContext.DestinationItems.AddFolder(copyItemContext.CopyPath,
                                                               Constants.vsProjectItemKindVirtualFolder);
                    break;

                case (Constants.vsProjectItemKindSubProject):
                default:
                    break;
                }
            }
            catch (Exception ce)
            {
                e = ce;
            }

            if (!copyItemContext.Recurse || null != e)
            {
                return;
            }

            var clone = copyItemContext.Clone();

            clone.DestinationItems = copyItemContext.Root.LastCreatedItem.ProjectItems;
            foreach (ProjectItem subItem in copyItemContext.Item.ProjectItems)
            {
                clone.Item     = subItem;
                clone.CopyPath = clone.Item.Name;
                AddItemCopy(clone, out e);
            }
        }
        private void AddItemCopy(CopyItemContext copyItemContext, out Exception e)
        {
            e = null;
            try
            {
                switch (copyItemContext.Item.Kind)
                {
                    case (Constants.vsProjectItemKindSolutionItems):
                    case (Constants.vsProjectItemKindPhysicalFile):
                        {
                            for (short i = 1; i <= copyItemContext.Item.FileCount; ++i)
                            {
                                var file = copyItemContext.Item.get_FileNames(i);
                                var newFileName = copyItemContext.CopyPath;

                                if (copyItemContext.IsWithinProject)
                                {
                                    copyItemContext.DestinationItems.AddFromTemplate(file, newFileName);
                                }
                                else
                                {
                                    copyItemContext.DestinationItems.AddFromFileCopy(file);
                                    copyItemContext.Root.LastCreatedItem.Name = newFileName;
                                }
                            }
                            break;
                        }
                    case (Constants.vsProjectItemKindPhysicalFolder):
                        copyItemContext.DestinationItems.AddFolder(copyItemContext.CopyPath,
                                                                   Constants.vsProjectItemKindPhysicalFolder);
                        break;
                    case (Constants.vsProjectItemKindVirtualFolder):
                        copyItemContext.DestinationItems.AddFolder(copyItemContext.CopyPath,
                                                                   Constants.vsProjectItemKindVirtualFolder);
                        break;
                    case (Constants.vsProjectItemKindSubProject):
                    default:
                        break;
                }
            }
            catch (Exception ce)
            {
                e = ce;
            }

            if (!copyItemContext.Recurse || null != e)
            {
                return;
            }

            var clone = copyItemContext.Clone();
            clone.DestinationItems = copyItemContext.Root.LastCreatedItem.ProjectItems;
            foreach (ProjectItem subItem in copyItemContext.Item.ProjectItems)
            {
                clone.Item = subItem;
                clone.CopyPath = clone.Item.Name;
                AddItemCopy(clone, out e);
            }
        }
        public IPathNode CopyItem(IContext context, string path, string copyPath, IPathNode destinationContainer,
                                  bool recurse)
        {
            ProjectItems     destinationItems   = null;
            ShellProject     destinationProject = destinationContainer.Item as ShellProject;
            ShellProjectItem destinationItem    = destinationContainer.Item as ShellProjectItem;

            if (null == destinationProject)
            {
                var containerKinds = new[]
                {
                    Constants.vsProjectItemKindPhysicalFolder,
                    Constants.vsProjectItemsKindSolutionItems,
                    Constants.vsProjectItemKindSubProject,
                    Constants.vsProjectItemKindVirtualFolder,
                };

                if (null == destinationItem || !containerKinds.Contains(destinationItem.Kind))
                {
                    throw new InvalidOperationException(
                              "Project items can only be moved or copied into a project node, a project folder, or solution folder.");
                }

                destinationItems   = destinationItem.AsProjectItem().ProjectItems;
                destinationProject = destinationItem.ContainingProject;
            }
            else
            {
                destinationItems = destinationProject.AsProject().ProjectItems;
            }

            ShellProject sourceProject = new ShellProject(_item.ContainingProject);

            bool isWithinProject = sourceProject.UniqueName == destinationProject.UniqueName;

            if (String.IsNullOrEmpty(copyPath))
            {
                if (isWithinProject)
                {
                    copyPath = "Copy of " + path;
                }
                else
                {
                    copyPath = path;
                }
            }

            ProjectItem     newItem     = null;
            CopyItemContext copyContext = null;
            var             events      = ((Events2)_item.DTE.Events);

            copyContext = new CopyItemContext(_item, copyPath, destinationItems, isWithinProject, recurse);

            events.ProjectItemsEvents.ItemAdded += copyContext.OnItemAdded;
            Exception error = null;

            AddItemCopy(copyContext, out error);

            events.ProjectItemsEvents.ItemAdded -= copyContext.OnItemAdded;
            if (null != error)
            {
                context.WriteError(new ErrorRecord(
                                       new CopyOrMoveItemInternalException(path, copyContext.CopyPath, error),
                                       "StudioShell.CopyItem.Internal", ErrorCategory.WriteError, path));
                return(null);
            }
            return(new PathNode(ShellObjectFactory.CreateFromProjectItem(newItem), copyPath, false));
        }
        public IPathNode CopyItem(IContext context, string path, string copyPath, IPathNode destinationContainer,
                                  bool recurse)
        {
            ProjectItems destinationItems = null;
            ShellProject destinationProject = destinationContainer.Item as ShellProject;
            ShellProjectItem destinationItem = destinationContainer.Item as ShellProjectItem;

            if (null == destinationProject)
            {
                var containerKinds = new[]
                                         {
                                             Constants.vsProjectItemKindPhysicalFolder,
                                             Constants.vsProjectItemsKindSolutionItems,
                                             Constants.vsProjectItemKindSubProject,
                                             Constants.vsProjectItemKindVirtualFolder,
                                         };

                if (null == destinationItem || !containerKinds.Contains(destinationItem.Kind))
                {
                    throw new InvalidOperationException(
                        "Project items can only be moved or copied into a project node, a project folder, or solution folder.");
                }

                destinationItems = destinationItem.AsProjectItem().ProjectItems;
                destinationProject = destinationItem.ContainingProject;
            }
            else
            {
                destinationItems = destinationProject.AsProject().ProjectItems;
            }

            ShellProject sourceProject = new ShellProject(_item.ContainingProject);

            bool isWithinProject = sourceProject.UniqueName == destinationProject.UniqueName;

            if (String.IsNullOrEmpty(copyPath))
            {
                if (isWithinProject)
                {
                    copyPath = "Copy of " + path;
                }
                else
                {
                    copyPath = path;
                }
            }

            ProjectItem newItem = null;
            CopyItemContext copyContext = null;
            var events = ((Events2) _item.DTE.Events);
            copyContext = new CopyItemContext(_item, copyPath, destinationItems, isWithinProject, recurse);

            events.ProjectItemsEvents.ItemAdded += copyContext.OnItemAdded;
            Exception error = null;
            AddItemCopy(copyContext, out error);

            events.ProjectItemsEvents.ItemAdded -= copyContext.OnItemAdded;
            if (null != error)
            {
                context.WriteError(new ErrorRecord(
                                       new CopyOrMoveItemInternalException(path, copyContext.CopyPath, error),
                                       "StudioShell.CopyItem.Internal", ErrorCategory.WriteError, path));
                return null;
            }
            return new PathNode(ShellObjectFactory.CreateFromProjectItem(newItem), copyPath, false);
        }