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

C# 重命名文件夹中的某些文件,c#,.net,C#,.net,我的任务是使用C#更改文件夹中某些文件的名称(即,为每个名称动态添加id) 示例:help.txt到1help.txt 我该怎么做呢?看一看 这样做: void RenameThem() { DirectoryInfo d = new DirectoryInfo("c:/dir/"); FileInfo[] infos = d.GetFiles("*.myfiles"); foreach(FileInfo f in infos) { // Do t

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

示例:help.txt到1help.txt

我该怎么做呢?

看一看

这样做:

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));
    }
}
string oldFilePath = Path.Combine( Server.MapPath("~/uploads"), "oldFileName");
string newFilePath = Path.Combine( Server.MapPath("~/uploads"), "newFileName");

File.Move(oldFilePath, newFilePath);

您要查找的函数是
System.IO
命名空间的
File.Move(源、目标)
。还可以查看
DirectoryInfo
类(相同名称空间)以访问文件夹的内容。

签出。我不知道它没有重命名。。。似乎您必须使用
System.IO.File.Move(oldFileName,newFileName)

我将在这里转储此文件,因为我需要为自己的目的编写此代码

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);
    }
}
您可以这样使用:

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));
    }
}
string oldFilePath = Path.Combine( Server.MapPath("~/uploads"), "oldFileName");
string newFilePath = Path.Combine( Server.MapPath("~/uploads"), "newFileName");

File.Move(oldFilePath, newFilePath);

在.NETFramework4.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);
   }
}
要重命名文件,我的方法如下所示

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

使用Path.Combine(f.Directory.ToString(),“1”+f.Name)而不是“1”+f.fullname System.IO.FileInfo上没有重命名方法@Chris这就是为什么Traveley为
FileInfo
@svick-doh编写了
Rename()
扩展方法的原因!这将教会我不要向下滚动并阅读所有代码。我的错。