如何在 .NET 中重命名文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19150333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rename a file in .NET?
提问by CJ7
Using System.IO.Fileor another .NETclass, 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 Movethe best way?
如果使用System.IO.File,是Move最好的方法吗?
回答by Theraot
Alternatives
备择方案
System.IO.File.Move
系统.IO.文件.移动
You can rename a file with System.IO.File.Movelike 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 ofHow to write contents of one file to another file?.
您可以在看到这样一个例子中接受的解决方案如何将一个文件的内容写入到另一个文件?.
Notes
笔记
Note 1: Others alternatives include: Microsoft.VisualBasic.FileSystem.Renameand Microsoft.VisualBasic.FileIO.FileSystem.RenameFile.
注 1:其他替代方案包括:Microsoft.VisualBasic.FileSystem.Rename和Microsoft.VisualBasic.FileIO.FileSystem.RenameFile。
Both Microsoft.VisualBasic.FileSystem.Renameand System.IO.File.Moveare equivalent, they do parameter checks, permissions checks and error handling around MoveFilefrom kernel32.
这两个Microsoft.VisualBasic.FileSystem.Rename和System.IO.File.Move是等价的,他们做的参数检查,权限检查和错误处理周围MoveFile的kernel32。
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 MoveFilefrom kernel32.
关于Microsoft.VisualBasic.FileIO.FileSystem.RenameFile,它将在给定完整路径和新名称的情况下重命名同一文件夹中的文件(类似于我上面介绍的构建目标路径的内容),然后回退到MoveFilefrom kernel32.
In a similar fashion System.IO.FileInfo.MoveTowill call MoveFilefrom kernel32.
以类似的方式System.IO.FileInfo.MoveTo将调用MoveFile来自kernel32。
Note 2: It should also be noted that you cannot find Microsoft.VisualBasicfrom Mono. That means that System.IO.File.Moveand System.IO.FileInfo.MoveToare the only portable options.
注 2:还应注意,您无法Microsoft.VisualBasic从 Mono 中找到。这意味着System.IO.File.Move并且System.IO.FileInfo.MoveTo是唯一的便携式选项。
It is all MoveFile.
这都是MoveFile。
As mentioned above all those methods will fallback to MoveFilefrom kernel32. MoveFilewill work for any mounted drive on the system (even network drives), although it should not be used to move from a volume to another. In that case it will be necessary to copy the file to the destination and remove the old file.
如上所述,所有这些方法都将回退到MoveFilefrom kernel32。MoveFile将适用于系统上任何已安装的驱动器(甚至是网络驱动器),尽管它不应用于从一个卷移动到另一个卷。在这种情况下,需要将文件复制到目的地并删除旧文件。
The best alternative
最好的选择
Since the Microsoft.VisualBasic.FileSystem.Renameand Microsoft.VisualBasic.FileIO.FileSystem.RenameFilemay not be used in other operating systems as they are not ported by Mono, I'll discard those.
由于Microsoft.VisualBasic.FileSystem.Rename和Microsoft.VisualBasic.FileIO.FileSystem.RenameFile可能不会在其他操作系统中使用,因为它们不是由 Mono 移植的,我将丢弃它们。
So, we are left with System.IO.File.Moveand System.IO.FileInfo.MoveTo. Between those System.IO.File.Movehas less overhead because it doesn't have to maintain the state of the FileInfoobject. Other than that they will work the same... so, if you are already using FileInfouse System.IO.FileInfo.MoveTootherwise System.IO.File.Move.
所以,我们只剩下System.IO.File.Moveand 了System.IO.FileInfo.MoveTo。它们之间的System.IO.File.Move开销较小,因为它不必维护FileInfo对象的状态。除此之外,它们的工作方式相同……所以,如果您已经在使用,请FileInfo使用System.IO.FileInfo.MoveToelse System.IO.File.Move。
And so, System.IO.File.Moveis the best option to rename files in .NET!(They didn't give us a whacky API).
因此,System.IO.File.Move是在 .NET 中重命名文件的最佳选择!(他们没有给我们一个古怪的 API)。
Celebrate!
庆祝!

