如何使用C#重命名文件?
请参考System.IO.File.Move,将文件重命名为新名称。
System.IO.File.Move("oldfilename", "newfilename");
System.IO.File.Move(oldNameFullPath, newNameFullPath);
/* Delete the file if exists, else no exception thrown. */
File.Delete(newFileName); // Delete the existing file if exists
File.Move(oldFileName,newFileName); // Rename the oldFileName into newFileName
或者用try catch语句包围它以避免异常。
只需要添加:
namespace System.IO
{
public static class ExtendedMethod
{
public static void Rename(this FileInfo fileInfo, string newName)
{
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}
}
}
然后...
FileInfo file = new FileInfo("c:\test.txt");
file.Rename("test2.txt");
第一种解决方案
避免使用这里发布的System.IO.File.Move
解决方案(包括标记答案)。它在网络上失败。但是复制/删除模式在本地和网络上都可以工作。跟随其中一种移动解决方案,但将其替换为Copy。然后使用File.Delete删除原始文件。
您可以创建一个Rename方法来简化它。
易用性
在C#中使用VB程序集。 添加对Microsoft.VisualBasic的引用
然后重命名文件:
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(myfile, newName);
两者都是字符串。请注意,myfile具有完整路径。newName没有。 例如:
a = "C:\whatever\a.txt";
b = "b.txt";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(a, b);
C:\whatever\
文件夹现在将包含b.txt
。
smb
)、ftp
、ssh
或者其他所有都有用于文件移动/重命名的命令/原语,除非不允许(例如只读)。 - Meow Cat 2012System.IO.File
类将其复制为新文件,然后删除旧文件:if (File.Exists(oldName))
{
File.Copy(oldName, newName, true);
File.Delete(oldName);
}
public void RenameFile(string filePath, string newName)
{
FileInfo fileInfo = new FileInfo(filePath);
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}
注意:在这个示例代码中,我们打开一个目录并搜索名称中带有括号的PDF文件。您可以检查和替换名称中的任何字符,或者只需使用替换函数指定一个全新的名称。
有其他方法可以从这段代码中进行更复杂的重命名,但我的主要意图是展示如何使用File.Move进行批量重命名。当我在我的笔记本电脑上运行它时,它对180个目录中的335个PDF文件起作用。这是即兴编写的代码,还有更复杂的方法可以实现它。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BatchRenamer
{
class Program
{
static void Main(string[] args)
{
var dirnames = Directory.GetDirectories(@"C:\the full directory path of files to rename goes here");
int i = 0;
try
{
foreach (var dir in dirnames)
{
var fnames = Directory.GetFiles(dir, "*.pdf").Select(Path.GetFileName);
DirectoryInfo d = new DirectoryInfo(dir);
FileInfo[] finfo = d.GetFiles("*.pdf");
foreach (var f in fnames)
{
i++;
Console.WriteLine("The number of the file being renamed is: {0}", i);
if (!File.Exists(Path.Combine(dir, f.ToString().Replace("(", "").Replace(")", ""))))
{
File.Move(Path.Combine(dir, f), Path.Combine(dir, f.ToString().Replace("(", "").Replace(")", "")));
}
else
{
Console.WriteLine("The file you are attempting to rename already exists! The file path is {0}.", dir);
foreach (FileInfo fi in finfo)
{
Console.WriteLine("The file modify date is: {0} ", File.GetLastWriteTime(dir));
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}
没有任何答案提到编写可单元测试的解决方案。您可以使用System.IO.Abstractions
,因为它提供了一个可测试的包装器,用于对文件系统操作进行包装。使用它,您可以创建模拟文件系统对象并编写单元测试。
using System.IO.Abstractions;
IFileInfo fileInfo = _fileSystem.FileInfo.FromFileName("filePathAndName");
fileInfo.MoveTo(Path.Combine(fileInfo.DirectoryName, newName));
这是经过测试的可用代码,用于重命名文件。