更改文件扩展名VB.NET

Sha*_*hin 4 .net vb.net

是否有任何功能在.NET中更改文件扩展名?或者我必须重命名文件?谢谢

例如,我想将扩展名为".resxx"的目录中的每个文件重命名为.resx.我的代码有什么问题?

Dim [option] As SearchOption = SearchOption.AllDirectories [option] = SearchOption.AllDirectories

    Dim fileNames As String() = Directory.GetFiles("C:\New Folder", "*.resxx", [option])
    For Each f In fileNames
        Dim t As New FileInfo(f.ToString)
        MsgBox(Mid(f, 1, f.Length - 4))
        t.MoveTo(Mid(f, 1, f.Length - 4) + ".resx")
    Next
Run Code Online (Sandbox Code Playgroud)

Ash*_*Ash 12

是的,有:Path.ChangeExtension

事实上,Path类通常具有一系列有用的文件/目录名称操作方法.令人惊讶的是,有多少开发人员不了解/使用它.


小智 5

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myFiles As String()

myFiles = IO.Directory.GetFiles("D:\Temp\", "*.txt")

Dim newFilePath As String

For Each filepath As String In myFiles

newFilePath = filepath.Replace(".txt", ".html")

System.IO.File.Move(filepath, newFilePath)

Next

End Sub

End Class
Run Code Online (Sandbox Code Playgroud)