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

2023-09-03 00:45:27 作者:没我的日子你别来狗恙吧

使用有System.IO.File 或其他 .NET 类,什么是重命名文件的最佳方法?

Using System.IO.File or another .NET class, what is the best way to rename a file?

我希望能够重新命名本地驱动器或网络位置的文件。

I want to be able to rename files on local drives or on network locations.

如果使用有System.IO.File 移动最好的方法是什么?

If using System.IO.File, is Move the best way?

推荐答案

System.IO.File.Move

您可以重命名为 System.IO.File.Move 这样的文件:

You can rename a file with System.IO.File.Move like this:

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

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.

手举

显然,你可以随时打开现有的文件,创建一个新的使用所需要的名字......从旧的复制比赛到新的,最后删除旧的。这将工作给你有写权限,但日期和安全信息(所有者文件等)不会被preserved。

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?.

注意的

注1 :其他替代方案包括: Microsoft.VisualBasic.FileSystem.Rename Microsoft.VisualBasic.FileIO。 FileSystem.RenameFile

Note 1: Others alternatives include: Microsoft.VisualBasic.FileSystem.Rename and Microsoft.VisualBasic.FileIO.FileSystem.RenameFile.

两个 Microsoft.VisualBasic.FileSystem.Rename System.IO.File.Move 是等价的,他们这样做参数检查,权限检查和错误处理周围的MoveFile从 KERNEL32

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.FileIO.FileSystem.RenameFile ,它会给予了充分的路径和新名称(类似我$ P在同一文件夹重命名文件以上$ psent建设目标路径),然后回落到 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 MoveFile from kernel32.

在以类似的方式 System.IO.FileInfo.MoveTo 将调用 MoveFile KERNEL32

In a similar fashion System.IO.FileInfo.MoveTo will call MoveFile from kernel32.

注2 :还应当指出的是,你不能找到 Microsoft.VisualBasic程序从单声道。这意味着, System.IO.File.Move System.IO.FileInfo.MoveTo 是唯一的便携式选项。

Note 2: It should also be noted that you cannot find Microsoft.VisualBasic from Mono. That means that System.IO.File.Move and System.IO.FileInfo.MoveTo are the only portable options.

正如上面提到的所有这些方法都将回退到 MoveFile KERNEL32 MoveFile 将工作系统(包括网络驱动器)上的任何安装的驱动器,但它不应该被用来从卷移动到另一个。在这种情况下将有必要将文件复制到目标并删除旧文件

As mentioned above all those methods will fallback to MoveFile from kernel32. 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. In that case it will be necessary to copy the file to the destination and remove the old file.

由于 Microsoft.VisualBasic.FileSystem.Rename Microsoft.VisualBasic.FileIO.FileSystem.RenameFile 不可以可以使用其他操作系统中,因为它们不是由单音移植,我将丢弃那些

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.

所以,我们剩下的 System.IO.File.Move System.IO.FileInfo.MoveTo 。之间的 System.IO.File.Move 开销较少,因为它不具备维持的FileInfo 对象。除此之外,他们将工作一样......所以,如果你已经使用的FileInfo 使用 System.IO.FileInfo.MoveTo ,否则 System.IO.File.Move

So, we are left with System.IO.File.Move and System.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. 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.

因此​​, System.IO.File.Move 是在.NET中重命名文件的最佳选择!(他们没有给我们一个怪诞的API)。

And so, System.IO.File.Move is the best option to rename files in .NET! (They didn't give us a whacky API).

庆祝!的