public static string GetUniqueFileName(string fullName) { // 如果不存在fullName指定的文件,直接返回它。 if (!System.IO.File.Exists(fullName)) return fullName; string dir = System.IO.Path.GetDirectoryName(fullName); string name = System.IO.Path.GetFileNameWithoutExtension(fullName); string ext = System.IO.Path.GetExtension(fullName); string unique; int index = 1;
do { unique = System.IO.Path.Combine(dir, string.Format("{0}[{1}].{2}", name, index++, ext)); } while (System.IO.File.Exists(unique)); return unique; }传入参数是你期望的文件全名,注意哦是全名。 代码随手写的,仅供参考,不保证能够正常使用。
public static string GetUniqueFileName(string fullName)
{
// 如果不存在fullName指定的文件,直接返回它。
if (!System.IO.File.Exists(fullName)) return fullName; string dir = System.IO.Path.GetDirectoryName(fullName);
string name = System.IO.Path.GetFileNameWithoutExtension(fullName);
string ext = System.IO.Path.GetExtension(fullName);
string unique;
int index = 1;
do {
unique = System.IO.Path.Combine(dir, string.Format("{0}[{1}].{2}", name, index++, ext));
} while (System.IO.File.Exists(unique)); return unique;
}传入参数是你期望的文件全名,注意哦是全名。
代码随手写的,仅供参考,不保证能够正常使用。
去看下,不知道对你有没有帮助
http://kb.cnblogs.com/a/1165129/代码是全的你看看