简体   繁体   English

如何在.NET中重命名文件?

[英]How to rename a file in .NET?

Using System.IO.File or another .NET class, what is the best way to rename a file? 使用System.IO.File或其他.NET类,重命名文件的最佳方法是什么?

I want to be able to rename files on local drives or on network locations. 我希望能够重命名本地驱动器或网络位置上的文件。

If using System.IO.File , is Move the best way? 如果使用System.IO.FileMove是最好的方法吗?


Alternatives 备择方案

System.IO.File.Move System.IO.File.Move

You can rename a file with System.IO.File.Move like this: 您可以使用System.IO.File.Move重命名文件,如下所示:

var sourcePath = @"C:\folder\oldname.txt";
var destinationPath = @"C:\folder\newname.txt";
File.Move(sourcePath, destinationPath);

You may be interested in keeping the same location and just change the name of the file. 您可能有兴趣保留相同的位置,只需更改文件的名称。 Sure that can be done: 当然可以做到:

var sourcePath = @"C:\folder\oldname.txt";
var newName = "newname.txt";
var directory = Path.GetDirectoryName(sourcePath);
var destinationPath = Path.Combine(directory, newName);
File.Move(sourcePath, destinationPath);

System.IO.FileInfo.MoveTo System.IO.FileInfo.MoveTo

You can also use System.IO.FileInfo.MoveTo : 您还可以使用System.IO.FileInfo.MoveTo

var sourcePath = @"C:\folder\oldname.txt";
var destinationPath = @"C:\folder\newname.txt";
FileInfo info = new FileInfo(sourcePath);
info.MoveTo(destinationPath);

Note : You can also build the destination path as shown above. 注意 :您还可以构建目标路径,如上所示。


"Hand move" “动手”

Evidently you can always open the existing file, create a new one with the desired name... copy the contests from the old to the new one, and finally delete the old one. 显然,您可以随时打开现有文件,创建一个具有所需名称的新文件...将比赛从旧比赛复制到新比赛,最后删除旧比赛。 This will work given you have write access, although dates and security info (owner of the file, etc...) will not be preserved. 这将有效,因为您具有写访问权限,但不会保留日期和安全信息(文件的所有者等)。

You can see an example of that at the accepted solution of How to write contents of one file to another file? 您可以在如何将一个文件的内容写入另一个文件 的已接受解决方案中看到该示例 .


Notes 笔记

Note 1 : Others alternatives include: Microsoft.VisualBasic.FileSystem.Rename and Microsoft.VisualBasic.FileIO.FileSystem.RenameFile . 注1 :其他替代方案包括: Microsoft.VisualBasic.FileSystem.RenameMicrosoft.VisualBasic.FileIO.FileSystem.RenameFile

Both Microsoft.VisualBasic.FileSystem.Rename and System.IO.File.Move are equivalent, they do parameter checks, permissions checks and error handling around MoveFile from kernel32 . Microsoft.VisualBasic.FileSystem.RenameSystem.IO.File.Move都是等效的,它们对来自kernel32 MoveFile进行参数检查,权限检查和错误处理。

Regarding Microsoft.VisualBasic.FileIO.FileSystem.RenameFile , it will rename files in the same folder given the full path and the new name (similar to what I present above to build the destination path), and then fall back to MoveFile from kernel32 . 关于Microsoft.VisualBasic.FileIO.FileSystem.RenameFile ,它将在给定完整路径和新名称的同一文件夹中重命名文件(类似于我上面提到的构建目标路径),然后从kernel32回退到MoveFile

In a similar fashion System.IO.FileInfo.MoveTo will call MoveFile from kernel32 . 以类似的方式, System.IO.FileInfo.MoveTo将从kernel32调用MoveFile

Note 2 : It should also be noted that you cannot find Microsoft.VisualBasic from Mono. 注意2 :还应注意,您无法从Mono中找到Microsoft.VisualBasic That means that System.IO.File.Move and System.IO.FileInfo.MoveTo are the only portable options. 这意味着System.IO.File.MoveSystem.IO.FileInfo.MoveTo是唯一的可移植选项。


It is all MoveFile. 这都是MoveFile。

As mentioned above all those methods will fallback to MoveFile from kernel32 . 如上所述,所有这些方法都将从kernel32MoveFile MoveFile will work for any mounted drive on the system (even network drives), although it should not be used to move from a volume to another. MoveFile适用于系统上的任何已安装的驱动器(甚至是网络驱动器),但它不应用于从卷移动到另一个卷。 In that case it will be necessary to copy the file to the destination and remove the old file. 在这种情况下,有必要将文件复制到目标并删除旧文件。


The best alternative 最好的选择

Since the Microsoft.VisualBasic.FileSystem.Rename and Microsoft.VisualBasic.FileIO.FileSystem.RenameFile may not be used in other operating systems as they are not ported by Mono, I'll discard those. 由于Microsoft.VisualBasic.FileSystem.RenameMicrosoft.VisualBasic.FileIO.FileSystem.RenameFile可能不会在其他操作系统中使用,因为它们没有被Mono移植,我将丢弃它们。

So, we are left with System.IO.File.Move and System.IO.FileInfo.MoveTo . 因此,我们留下了System.IO.File.MoveSystem.IO.FileInfo.MoveTo Between those System.IO.File.Move has less overhead because it doesn't have to maintain the state of the FileInfo object. 在那些System.IO.File.Move之间具有较少的开销,因为它不必维护FileInfo对象的状态。 Other than that they will work the same... so, if you are already using FileInfo use System.IO.FileInfo.MoveTo otherwise System.IO.File.Move . 除此之外,它们将工作相同...所以,如果您已经在使用FileInfo使用System.IO.FileInfo.MoveTo否则使用System.IO.File.Move

And so, System.IO.File.Move is the best option to rename files in .NET! 因此, System.IO.File.Move是在.NET中重命名文件的最佳选择! (They didn't give us a whacky API). (他们没有给我们一个糟糕的API)。

Celebrate! 庆祝!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM