Rex*_*x M 11
这是我经常用于这种情况的方法.只需传入一个像"截图"这样的字符串,它会找到"Screenshot [number]"格式的最低可用文件名(如果还没有,则只显示"截图"):
private string GetUniqueName(string name, string folderPath)
{
string validatedName = name;
int tries = 1;
while (File.Exists(folderPath + validatedName))
{
validatedName = string.Format("{0} [{1}]", name, tries++);
}
return validatedName;
}
Run Code Online (Sandbox Code Playgroud)
(注意:这是一个略微简化的版本,不考虑文件扩展名).