using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
public class FolderNameHelper
{
private List<string> nameList;
public FolderNameHelper(TreeNode parentNode)
{
nameList = new List<string>();
if (parentNode != null && parentNode.Nodes != null)
{
foreach (TreeNode node in parentNode.Nodes)
{
nameList.Add( node.Text.ToLower().Trim() );
}
}
}
public FolderNameHelper()
{
}
public bool IsUniqueObjectName(string objectName)
{
if (null == objectName || objectName.Trim().Length == 0)
{
return false;
}
return !nameList.Contains(objectName.ToLower().Trim());
}
public string GenerateObjectName(string oldName)
{
string[] names = GenerateUniqueNames(oldName, 1, 2);
if (names != null && names.Length > 0)
{
return names[0];
}
return null;
}
private string[] GenerateUniqueNames(string objectName, int count, int start)
{
string[] resultNames = new string[count];
string tempName;
int index = 0;
if (this.IsUniqueObjectName(objectName))
{
resultNames[0] = objectName;
index++;
}
int appendix = start;
while (index < count)
{
tempName = objectName + " (" + appendix + ")";
if (IsUniqueObjectName(tempName))
{
resultNames[index] = tempName;
index++;
}
appendix++;
}
return resultNames;
}
}