在文件夹中重命名一些文件

57

我有一个任务,需要使用C#在文件夹中更改一些文件的名称(即为每个名称动态添加id)。

例如:将help.txt更改为1help.txt。

我该如何完成这个任务?

6个回答

86

看一下FileInfo

按照以下方式操作:

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));
    }
}

15
建议使用Path.Combine(f.Directory.ToString(), "1" + f.Name)代替"1" + f.FullName。这样做可以保持原有的意思,并使代码更加易读和易懂。 - roman m

9

我会在这里放置一些代码,因为我需要为自己的目的编写此代码。

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);
    }
}

2
System.IO.FileInfo 上没有重命名方法! - Chris
4
这就是为什么Journey为FileInfo编写了Rename()扩展方法的原因。 - svick
@svick 哎呀!这会教训我不滚动并阅读所有代码。我的错。 - Chris

8
您要查找的功能是System.IO命名空间中的File.Move(source,destination)函数。此外,请查看同一命名空间的DirectoryInfo类以访问文件夹的内容。

3

在 .NET Framework 4.0 上,我使用只需要一个参数的 FileInfo.MoveTo() 方法

仅用于移动文件时,我的方法看起来像这样:

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);
    }
}

重命名文件的方法如下:

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);
   }
}

正如您所看到的,重命名文件的语法与移动文件的语法几乎相同,只需要在使用MoveTo()方法之前修改filename即可。


2

0
你可以像这样使用File.Move
string oldFilePath = Path.Combine( Server.MapPath("~/uploads"), "oldFileName");
string newFilePath = Path.Combine( Server.MapPath("~/uploads"), "newFileName");

File.Move(oldFilePath, newFilePath);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接