如果指定的文件名已经存在的方式来获得唯一的文件名(.NET)文件名、存在、方式、NET

2023-09-03 05:20:52 作者:曾经的曾经都已是曾经

有一个内置的.NET功能,如果一个文件名已经存在,获得唯一的文件名?所以,如果我尽力挽救 MYDOC.DOC 键,它已经存在,该文件将保存名为 MyDoc(1)的.doc ,以同样的方式在浏览器下载适用于例如。

Is there a built in .NET function to get a unique filename if a filename already exists? So if I try and save MyDoc.doc and it already exists, the file will save with name MyDoc(1).doc, the same way a browser download works for example.

如果没有,什么是实现这一结果的最有效方法是什么?

If not, what is the most efficient way to achieve this result?

我现在用的是 File.Move 功能目前BTW。

I am using the File.Move function at the moment btw.

推荐答案

核对正则表达式的名字* \(\ D + \),如果它不匹配,加入(1),如果匹配增加括号内的数字。

check the name against Regex *.\(\d+\), if it doesn't match, add (1), if it matches increment the number in brackets.