示例#1
0
        /// <summary>
        /// Renames a file node.
        /// </summary>
        /// <param name="label">The new name.</param>
        /// <returns>An errorcode for failure or S_OK.</returns>
        /// <exception cref="InvalidOperationException" if the file cannot be validated>
        /// <devremark>
        /// We are going to throw instead of showing messageboxes, since this method is called from various places where a dialog box does not make sense.
        /// For example the FileNodeProperties are also calling this method. That should not show directly a messagebox.
        /// Also the automation methods are also calling SetEditLabel
        /// </devremark>

        public override int SetEditLabel(string label)
        {
            // IMPORTANT NOTE: This code will be called when a parent folder is renamed. As such, it is
            //                 expected that we can be called with a label which is the same as the current
            //                 label and this should not be considered a NO-OP.
            if (this.ProjectMgr == null || this.ProjectMgr.IsClosed)
            {
                return(VSConstants.E_FAIL);
            }

            // Validate the filename.
            if (String.IsNullOrEmpty(label))
            {
                throw new InvalidOperationException(SR.GetString(SR.ErrorInvalidFileName, label));
            }
            else if (label.Length > NativeMethods.MAX_PATH)
            {
                throw new InvalidOperationException(SR.GetString(SR.PathTooLong, label));
            }
            else if (Utilities.IsFileNameInvalid(label))
            {
                throw new InvalidOperationException(SR.GetString(SR.ErrorInvalidFileName, label));
            }

            for (HierarchyNode n = this.Parent.FirstChild; n != null; n = n.NextSibling)
            {
                // TODO: Distinguish between real Urls and fake ones (eg. "References")
                if (n != this && String.Equals(n.Caption, label, StringComparison.OrdinalIgnoreCase))
                {
                    if (File.Exists(n.Url))
                    {
                        //A file or folder with the name '{0}' already exists on disk at this location. Please choose another name.
                        //If this file or folder does not appear in the Solution Explorer, then it is not currently part of your project. To view files which exist on disk, but are not in the project, select Show All Files from the Project menu.
                        throw new InvalidOperationException(SR.GetString(SR.FileOrFolderAlreadyExists, label));
                    }
                    else
                    {
                        // Check if the file is open in the editor, if so, we need to close it, and if it's dirty
                        // let the user save it.
                        DocumentManager manager = n.GetDocumentManager();
                        if (manager != null)
                        {
                            int close = manager.Close(__FRAMECLOSE.FRAMECLOSE_PromptSave);
                            if (close == VSConstants.E_ABORT || close == VSConstants.S_FALSE)
                            {
                                // User cancelled operation in message box.
                                throw new InvalidOperationException(SR.GetString(SR.FileOpenDoesNotExist, label));
                            }
                        }

                        if (File.Exists(n.Url))
                        {
                            // The file was dirty and the user saved it.
                            throw new InvalidOperationException(SR.GetString(SR.FileOrFolderAlreadyExists, label));
                        }

                        // The file is no longer open in the editor and isn't on disk.  We can try removing it now.
                        if (!n.Remove(false))
                        {
                            throw new InvalidOperationException(SR.GetString(SR.UnableToRemoveFile, label));
                        }
                    }
                }
            }

            string fileName = Path.GetFileNameWithoutExtension(label);

            // Verify that the file extension is unchanged
            string strRelPath = Path.GetFileName(this.ItemNode.GetMetadata(ProjectFileConstants.Include));

            if (!Utilities.IsInAutomationFunction(this.ProjectMgr.Site) &&
                !String.Equals(Path.GetExtension(strRelPath), Path.GetExtension(label), StringComparison.OrdinalIgnoreCase))
            {
                // Prompt to confirm that they really want to change the extension of the file
                string     message = SR.GetString(SR.ConfirmExtensionChange, label);
                IVsUIShell shell   = this.ProjectMgr.Site.GetService(typeof(SVsUIShell)) as IVsUIShell;

                Utilities.CheckNotNull(shell, "Could not get the UI shell from the project");

                if (!VsShellUtilities.PromptYesNo(message, null, OLEMSGICON.OLEMSGICON_INFO, shell))
                {
                    // The user cancelled the confirmation for changing the extension.
                    // Return S_OK in order not to show any extra dialog box
                    return(VSConstants.S_OK);
                }
            }


            // Build the relative path by looking at folder names above us as one scenarios
            // where we get called is when a folder above us gets renamed (in which case our path is invalid)
            HierarchyNode parent = this.Parent;

            while (parent != null && (parent is FolderNode))
            {
                strRelPath = Path.Combine(parent.Caption, strRelPath);
                parent     = parent.Parent;
            }

            return(SetEditLabel(label, strRelPath));
        }