重命名文件夹中的某些文件

时间:2009-03-25 09:10:26

标签: c# .net

我的任务是使用C#更改某些文件的名称(即,动态地为每个名称添加id)。

示例:help.txt到1help.txt

我该怎么做?

6 个答案:

答案 0 :(得分: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));
    }
}

答案 1 :(得分: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 :(得分:8)

您要查找的函数是File.Move(source, destination)命名空间的System.IO。另请查看DirectoryInfo类(具有相同名称空间)以访问该文件夹的内容。

答案 3 :(得分:1)

查看 How can I rename a file in C#? 。我不知道C#没有重命名......似乎你必须使用System.IO.File.Move(oldFileName, newFileName)

答案 4 :(得分:0)

您可以使用File.Move,如下所示:

string oldFilePath = Path.Combine( Server.MapPath("~/uploads"), "oldFileName");
string newFilePath = Path.Combine( Server.MapPath("~/uploads"), "newFileName");

File.Move(oldFilePath, newFilePath);

答案 5 :(得分:0)

在.NET Framework 4.0上,我使用只需要1个参数的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);
   }
}

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