コード例 #1
0
        private void RenameFolder(string newName)
        {
            string newPath     = Path.Combine(this.Parent.VirtualNodeName, newName);
            string newFullPath = Path.Combine(this.ProjectMgr.ProjectFolder, newPath);

            // Only do the physical rename if the leaf name changed
            if (String.Compare(Path.GetFileName(VirtualNodeName), newName, StringComparison.Ordinal) != 0)
            {
                // Verify that no directory/file already exists with the new name on disk.
                // If it does, just subsume that name if our directory is empty.
                if (Directory.Exists(newFullPath) || FSLib.Shim.FileSystem.SafeExists(newFullPath))
                {
                    // We can't delete our old directory as it is not empty
                    if (Directory.EnumerateFileSystemEntries(this.Url).Any())
                    {
                        ShowErrorMessage(SR.FolderCannotBeRenamed, newPath);
                        return;
                    }

                    // Try to delete the old (empty) directory.
                    // Note that we don't want to delete recursively in case a file was added between
                    // when we checked and when we went to delete (potential race condition).
                    Directory.Delete(this.Url, false);
                }
                else
                {
                    this.RenameDirectory(newFullPath);
                }
            }

            this.VirtualNodeName = newPath;

            this.ItemNode.Rename(VirtualNodeName);

            // Let all children know of the new path
            for (HierarchyNode child = this.FirstChild; child != null; child = child.NextSibling)
            {
                if (!(child is FolderNode))
                {
                    child.SetEditLabel(child.Caption);
                }
                else
                {
                    FolderNode childFolderNode = (FolderNode)child;
                    childFolderNode.RenameFolder(childFolderNode.Caption);
                }
            }

            // Some of the previous operation may have changed the selection so set it back to us
            IVsUIHierarchyWindow uiWindow = UIHierarchyUtilities.GetUIHierarchyWindow(this.ProjectMgr.Site, SolutionExplorer);

            ErrorHandler.ThrowOnFailure(uiWindow.ExpandItem(this.ProjectMgr.InteropSafeIVsUIHierarchy, this.ID, EXPANDFLAGS.EXPF_SelectItem));
        }