C# 重命名文件夹中的一些文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/680786/
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
Rename some files in a folder
提问by
I have a task of changing the names of some files (that is, adding id to each name dynamically) in a folder using C#.
我的任务是使用 C# 更改文件夹中某些文件的名称(即,动态地为每个名称添加 id)。
Example: help.txt to 1help.txt
示例:help.txt 到 1help.txt
How can I do this?
我怎样才能做到这一点?
回答by Daniel
Have a look at FileInfo.
看看FileInfo。
Do something like this:
做这样的事情:
void RenameThem()
{
DirectoryInfo d = new DirectoryInfo("c:/dir/");
FileInfo[] infos = d.GetFiles("*.myfiles");
foreach(FileInfo f in infos)
{
// Do the renaming here
File.Move(f.FullName, Path.Combine(f.DirectoryName, "1" + f.Name));
}
}
回答by Asuah
The function that you are looking for is File.Move(source, destination)of the System.IOnamespace. Also take a look at the DirectoryInfoclass (of the same namespace) to access the contents of the folder.
您正在寻找的功能是File.Move(source, destination)对的System.IO命名空间。还要看一下DirectoryInfo(相同命名空间的)类来访问文件夹的内容。
回答by Gishu
Check out How can I rename a file in C#?. I didn't know that C#doesn't have a rename... It seems you have to use System.IO.File.Move(oldFileName, newFileName)
查看如何在 C# 中重命名文件?. 我不知道C#没有重命名...看来您必须使用System.IO.File.Move(oldFileName, newFileName)
回答by Journey
I'll just dump this here since I needed to write this code for my own purposes.
由于我需要为自己的目的编写此代码,因此我将把它转储到这里。
using System;
using System.IO;
public static class FileSystemInfoExtensions
{
public static void Rename(this FileSystemInfo item, string newName)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
FileInfo fileInfo = item as FileInfo;
if (fileInfo != null)
{
fileInfo.Rename(newName);
return;
}
DirectoryInfo directoryInfo = item as DirectoryInfo;
if (directoryInfo != null)
{
directoryInfo.Rename(newName);
return;
}
throw new ArgumentException("Item", "Unexpected subclass of FileSystemInfo " + item.GetType());
}
public static void Rename(this FileInfo file, string newName)
{
// Validate arguments.
if (file == null)
{
throw new ArgumentNullException("file");
}
else if (newName == null)
{
throw new ArgumentNullException("newName");
}
else if (newName.Length == 0)
{
throw new ArgumentException("The name is empty.", "newName");
}
else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0
|| newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0)
{
throw new ArgumentException("The name contains path separators. The file would be moved.", "newName");
}
// Rename file.
string newPath = Path.Combine(file.DirectoryName, newName);
file.MoveTo(newPath);
}
public static void Rename(this DirectoryInfo directory, string newName)
{
// Validate arguments.
if (directory == null)
{
throw new ArgumentNullException("directory");
}
else if (newName == null)
{
throw new ArgumentNullException("newName");
}
else if (newName.Length == 0)
{
throw new ArgumentException("The name is empty.", "newName");
}
else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0
|| newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0)
{
throw new ArgumentException("The name contains path separators. The directory would be moved.", "newName");
}
// Rename directory.
string newPath = Path.Combine(directory.Parent.FullName, newName);
directory.MoveTo(newPath);
}
}
回答by Journey
回答by Journey
On .NET Framework 4.0 I use FileInfo.MoveTo()method that only takes 1 argument
在 .NET Framework 4.0 上,我使用FileInfo.MoveTo()只接受 1 个参数的方法
Just to move files my method looks like this
只是为了移动文件我的方法看起来像这样
private void Move(string sourceDirName, string destDirName)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
FileInfo[] files = null;
files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.MoveTo(temppath);
}
}
to rename files my method looks like this
重命名文件我的方法看起来像这样
private void Rename(string folderPath)
{
int fileCount = 0;
DirectoryInfo dir = new DirectoryInfo(folderPath);
files = dir.GetFiles();
foreach (FileInfo file in files)
{
fileCount += 1;
string newFileName = fileCount.ToString() + file.Name;
string temppath = Path.Combine(folderPath, newFileName);
file.MoveTo(temppath);
}
}
AS you can see to Rename file it syntax is almost the same as to Move it, just need to modify the filenamebefore using MoveTo()method.
如你所见,Rename file 的语法与Move it 几乎相同,只需要修改filenamebefore usingMoveTo()方法。

