public static string GetFreeFileName(string baseFileName, string extension, int extraCharsRequired = 0)
{
int maxPath = _maxPath - extraCharsRequired;
if (!String.IsNullOrEmpty(extension) && !extension.StartsWith("."))
{
extension = "." + extension;
}
if (baseFileName.Length > maxPath - extension.Length - 1)
{
baseFileName = baseFileName.Substring(0, maxPath - extension.Length - 1);
}
while (baseFileName.Length < 4)
{
baseFileName += '_';
}
string fileName = baseFileName + extension;
int count = 1;
while (File.Exists(fileName)
&& count < 256 * 8)
{
fileName = baseFileName.Substring(0, baseFileName.Length - 3) + count.ToString("X", CultureInfo.InvariantCulture).PadLeft(3, '_') + extension;
count++;
}
return fileName;
}