protected internal virtual void AddFolderFromOtherProject(string folderToAdd, HierarchyNode targetNode)
{
if (string.IsNullOrEmpty(folderToAdd))
throw new ArgumentNullException("folderToAdd");
if (targetNode == null)
throw new ArgumentNullException("targetNode");
// Split the reference in its 3 parts
var index1 = Guid.Empty.ToString("B").Length;
if (index1 + 1 >= folderToAdd.Length)
throw new ArgumentOutOfRangeException("folderToAdd");
// Get the Guid
var guidString = folderToAdd.Substring(1, index1 - 2);
var projectInstanceGuid = new Guid(guidString);
// Get the project path
var index2 = folderToAdd.IndexOf('|', index1 + 1);
if (index2 < 0 || index2 + 1 >= folderToAdd.Length)
throw new ArgumentOutOfRangeException("folderToAdd");
// Finally get the source path
var folder = folderToAdd.Substring(index2 + 1);
// Get the target path
var folderName = Path.GetFileName(Path.GetDirectoryName(folder));
var targetPath = Path.Combine(GetBaseDirectoryForAddingFiles(targetNode), folderName);
// Recursively copy the directory to the new location
Utilities.RecursivelyCopyDirectory(folder, targetPath);
// Retrieve the project from which the items are being copied
IVsHierarchy sourceHierarchy;
var solution = (IVsSolution) GetService(typeof (SVsSolution));
ErrorHandler.ThrowOnFailure(solution.GetProjectOfGuid(ref projectInstanceGuid, out sourceHierarchy));
// Then retrieve the item ID of the item to copy
var itemID = VSConstants.VSITEMID_ROOT;
ErrorHandler.ThrowOnFailure(sourceHierarchy.ParseCanonicalName(folder, out itemID));
// Ensure we don't end up in an endless recursion
if (Utilities.IsSameComObject(InteropSafeIVsHierarchy, sourceHierarchy))
{
var cursorNode = targetNode;
while (cursorNode != null)
{
if (string.Compare(folder, cursorNode.GetMkDocument(), StringComparison.OrdinalIgnoreCase) == 0)
throw new Exception();
cursorNode = cursorNode.Parent;
}
}
// Now walk the source project hierarchy to see which node needs to be added.
WalkSourceProjectAndAdd(sourceHierarchy, itemID, targetNode, false);
}