文件名复制算法

时间:2010-02-02 14:52:54

标签: algorithm

当我在Windows中的同一目录中复制以下文件时。

“Log.txt”它被复制为“Log of Log.txt” 如果现在再次复制“Copy of Log.txt”,它将被复制为“Log.txt副本的副本” 如果现在再次复制“Log of Log.txt”,它将被复制为“Log.txt副本的副本(2)”

任何人都知道这里使用了什么算法。

1 个答案:

答案 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);