/// <summary> /// Renames the file in the hierarchy by removing old node and adding a new node in the hierarchy. /// </summary> /// <param name="oldFileName">The old file name.</param> /// <param name="newFileName">The new file name</param> /// <param name="newParentId">The new parent id of the item.</param> /// <returns>The newly added FileNode.</returns> /// <remarks>While a new node will be used to represent the item, the underlying MSBuild item will be the same and as a result file properties saved in the project file will not be lost.</remarks> internal FileNode RenameFileNode(string oldFileName, string newFileName, HierarchyNode newParent) { if (CommonUtils.IsSamePath(oldFileName, newFileName)) { // We do not want to rename the same file return(null); } //If we are included in the project and our parent isn't then //we need to bring our parent into the project if (!this.IsNonMemberItem && newParent.IsNonMemberItem) { ErrorHandler.ThrowOnFailure(newParent.IncludeInProjectWithRefresh(false)); } // Retrieve child nodes to add later. List <HierarchyNode> childNodes = this.GetChildNodes(); FileNode renamedNode; using (this.ProjectMgr.ExtensibilityEventsDispatcher.Suspend()) { // Remove this from its parent. ProjectMgr.OnItemDeleted(this); this.Parent.RemoveChild(this); // Update name in MSBuild this.ItemNode.Rename(CommonUtils.GetRelativeFilePath(ProjectMgr.ProjectHome, newFileName)); // Request a new file node be made. This is used to replace the old file node. This way custom // derived FileNode types will be used and correctly associated on rename. This is useful for things // like .txt -> .js where the file would now be able to be a startup project/file. renamedNode = this.ProjectMgr.CreateFileNode(this.ItemNode); renamedNode.ItemNode.RefreshProperties(); renamedNode.UpdateCaption(); newParent.AddChild(renamedNode); renamedNode.Parent = newParent; } UpdateCaption(); ProjectMgr.ReDrawNode(renamedNode, UIHierarchyElement.Caption); renamedNode.ProjectMgr.ExtensibilityEventsDispatcher.FireItemRenamed(this, oldFileName); //Update the new document in the RDT. DocumentManager.RenameDocument(renamedNode.ProjectMgr.Site, oldFileName, newFileName, renamedNode.ID); //Select the new node in the hierarchy renamedNode.ExpandItem(EXPANDFLAGS.EXPF_SelectItem); // Add children to new node and rename them appropriately. childNodes.ForEach(x => renamedNode.AddChild(x)); RenameChildNodes(renamedNode); return(renamedNode); }
/// <summary> /// Renames the file in the hierarchy by removing old node and adding a new node in the hierarchy. /// </summary> /// <param name="oldFileName">The old file name.</param> /// <param name="newFileName">The new file name</param> /// <param name="newParentId">The new parent id of the item.</param> /// <returns>The newly added FileNode.</returns> /// <remarks>While a new node will be used to represent the item, the underlying MSBuild item will be the same and as a result file properties saved in the project file will not be lost.</remarks> internal FileNode RenameFileNode(string oldFileName, string newFileName, HierarchyNode newParent) { if (CommonUtils.IsSamePath(oldFileName, newFileName)) { // We do not want to rename the same file return null; } //If we are included in the project and our parent isn't then //we need to bring our parent into the project if (!this.IsNonMemberItem && newParent.IsNonMemberItem) { ErrorHandler.ThrowOnFailure(newParent.IncludeInProjectWithRefresh(false)); } // Retrieve child nodes to add later. List<HierarchyNode> childNodes = this.GetChildNodes(); FileNode renamedNode; using (this.ProjectMgr.ExtensibilityEventsDispatcher.Suspend()) { // Remove this from its parent. ProjectMgr.OnItemDeleted(this); this.Parent.RemoveChild(this); // Update name in MSBuild this.ItemNode.Rename(CommonUtils.GetRelativeFilePath(ProjectMgr.ProjectHome, newFileName)); // Request a new file node be made. This is used to replace the old file node. This way custom // derived FileNode types will be used and correctly associated on rename. This is useful for things // like .txt -> .js where the file would now be able to be a startup project/file. renamedNode = this.ProjectMgr.CreateFileNode(this.ItemNode); renamedNode.ItemNode.RefreshProperties(); renamedNode.UpdateCaption(); newParent.AddChild(renamedNode); renamedNode.Parent = newParent; } UpdateCaption(); ProjectMgr.ReDrawNode(renamedNode, UIHierarchyElement.Caption); renamedNode.ProjectMgr.ExtensibilityEventsDispatcher.FireItemRenamed(this, oldFileName); //Update the new document in the RDT. DocumentManager.RenameDocument(renamedNode.ProjectMgr.Site, oldFileName, newFileName, renamedNode.ID); //Select the new node in the hierarchy renamedNode.ExpandItem(EXPANDFLAGS.EXPF_SelectItem); // Add children to new node and rename them appropriately. childNodes.ForEach(x => renamedNode.AddChild(x)); RenameChildNodes(renamedNode); return renamedNode; }