当我在Windows中的同一目录中复制以下文件时。
“Log.txt”它被复制为“Log of Log.txt” 如果现在再次复制“Copy of Log.txt”,它将被复制为“Log.txt副本的副本” 如果现在再次复制“Log of Log.txt”,它将被复制为“Log.txt副本的副本(2)”
任何人都知道这里使用了什么算法。
答案 0 :(得分:9)
这很简单:
// source is string representing path of source file to copy
string dest = "Copy of " + source;
int count = 2;
while(File.Exists(dest)) {
dest = "Copy (" + count.ToString() + ") of " + source;
count++;
}
File.Copy(source, dest);