我如何重命名一个文件使用c# ?


当前回答

System.IO.File.Move(oldNameFullPath, newNameFullPath);

其他回答

看一下System.IO.File。移动,“移动”文件到一个新名称。

System.IO.File.Move("oldfilename", "newfilename");
public static class ImageRename
{
    public static void ApplyChanges(string fileUrl,
                                    string temporaryImageName,
                                    string permanentImageName)
    {
        var currentFileName = Path.Combine(fileUrl,
                                           temporaryImageName);

        if (!File.Exists(currentFileName))
            throw new FileNotFoundException();

        var extention = Path.GetExtension(temporaryImageName);
        var newFileName = Path.Combine(fileUrl,
                                       $"{permanentImageName}
                                         {extention}");

        if (File.Exists(newFileName))
            File.Delete(newFileName);

        File.Move(currentFileName, newFileName);
    }
}
System.IO.File.Move(oldNameFullPath, newNameFullPath);

您可以使用File。行动起来吧。

public void RenameFile(string filePath, string newName)
{
    FileInfo fileInfo = new FileInfo(filePath);
    fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}