VsTeXProject.VisualStudio.Project.ProjectNode.GenerateUniqueItemName C# (CSharp) Method

GenerateUniqueItemName() public method

for now used by add folder. Called on the ROOT, as only the project should need to implement this. for folders, called with parent folder, blank extension and blank suggested root
public GenerateUniqueItemName ( uint itemIdLoc, string ext, string suggestedRoot, string &itemName ) : int
itemIdLoc uint
ext string
suggestedRoot string
itemName string
return int
        public virtual int GenerateUniqueItemName(uint itemIdLoc, string ext, string suggestedRoot, out string itemName)
        {
            var rootName = string.Empty;
            var extToUse = string.Empty;
            itemName = string.Empty;

            //force new items to have a number
            var cb = 1;
            var found = false;
            var fFolderCase = false;
            var parent = NodeFromItemId(itemIdLoc);

            if (!string.IsNullOrEmpty(ext))
            {
                extToUse = ext.Trim();
            }

            if (!string.IsNullOrEmpty(suggestedRoot))
            {
                suggestedRoot = suggestedRoot.Trim();
            }

            if (suggestedRoot == null || suggestedRoot.Length == 0)
            {
                // foldercase, we assume... 
                suggestedRoot = "NewFolder";
                fFolderCase = true;
            }

            while (!found)
            {
                rootName = suggestedRoot;
                if (cb > 0)
                    rootName += cb.ToString(CultureInfo.CurrentCulture);

                if (extToUse.Length > 0)
                {
                    rootName += extToUse;
                }

                cb++;
                found = true;
                for (var n = parent.FirstChild; n != null; n = n.NextSibling)
                {
                    if (rootName == n.GetEditLabel())
                    {
                        found = false;
                        break;
                    }

                    //if parent is a folder, we need the whole url
                    var parentFolder = parent.Url;
                    if (parent is ProjectNode)
                        parentFolder = Path.GetDirectoryName(parent.Url);

                    var checkFile = Path.Combine(parentFolder, rootName);

                    if (fFolderCase)
                    {
                        if (Directory.Exists(checkFile))
                        {
                            found = false;
                            break;
                        }
                    }
                    else
                    {
                        if (File.Exists(checkFile))
                        {
                            found = false;
                            break;
                        }
                    }
                }
            }

            itemName = rootName;
            return VSConstants.S_OK;
        }
ProjectNode