重命名现有文件名

用户名

我有以下代码,它将文件复制到特定文件夹,然后重命名。当具有该名称的文件已经存在时,会出现以下异常:

Cannot create a file when that file already exists

有没有办法覆盖文件并重命名?还是我应该删除旧的,然后更改名称?

这是我的代码:

 File.Copy(FileLocation, NewFileLocation, true);
 //Rename:
 File.Move(Path.Combine(NewFileLocation, fileName), Path.Combine(NewFileLocation, "File.txt"));               
只有一个好奇的心

尝试仅使用:

if (File.Exists("newfilename"))
{
    System.IO.File.Delete("newfilename");
}

System.IO.File.Move("oldfilename", "newfilename");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章