Microsoft.VisualStudio.Project.ProjectElement.Rename C# (CSharp) Method

Rename() public method

public Rename ( string newPath ) : void
newPath string
return void
        public void Rename(string newPath)
        {
            if (newPath == null)
                throw new ArgumentNullException("newPath");
            if (string.IsNullOrEmpty(newPath))
                throw new ArgumentException("newPath cannot be null or empty");
            if (HasItemBeenDeleted)
                throw new InvalidOperationException("The item has been deleted.");

            string escapedPath = Microsoft.Build.Evaluation.ProjectCollection.Escape(newPath);
            if(this.IsVirtual)
            {
                virtualProperties[ProjectFileConstants.Include] = escapedPath;
                return;
            }

            Item.Rename(escapedPath);
            this.RefreshProperties();
        }

Usage Example

        private static void AddNonMemberFileItems(XProjectNode project, IList <string> fileList)
        {
            Utilities.ArgumentNotNull("fileList", fileList);

            foreach (string fileKey in fileList)
            {
                HierarchyNode parentNode = project;
                string[]      pathItems  = fileKey.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });

                if (String.Equals(fileKey, project.ProjectFile, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                XFolderNode topFolderNode = null;
                foreach (string fileOrDir in pathItems)
                {
                    string   childNodeId   = Path.Combine(parentNode.VirtualNodeName, fileOrDir);
                    FileInfo fileOrDirInfo = new FileInfo(Path.Combine(project.ProjectFolder, childNodeId));
                    if ((fileOrDirInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    {
                        break;
                    }

                    //HierarchyNode childNode = project.FindURL(fileOrDirInfo.FullName);
                    HierarchyNode childNode = parentNode.FindChild(childNodeId);
                    if (childNode == null)
                    {
                        if (topFolderNode == null)
                        {
                            topFolderNode = parentNode as XFolderNode;
                            if (topFolderNode != null && (!topFolderNode.IsNonMemberItem) && topFolderNode.IsExpanded)
                            {
                                topFolderNode = null;
                            }
                        }

                        ProjectElement element = new ProjectElement(project, null, true);
                        element.Rename(childNodeId);
                        element.SetMetadata(ProjectFileConstants.Name, childNodeId);
                        childNode = project.CreateFileNode(element);
                        parentNode.AddChild(childNode);
                        break;
                    }

                    parentNode = childNode;
                }
                ThreadHelper.ThrowIfNotOnUIThread();

                if (topFolderNode != null)
                {
                    topFolderNode.CollapseFolder();
                }
            }
        }
All Usage Examples Of Microsoft.VisualStudio.Project.ProjectElement::Rename