Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change File Extension VB.NET

Tags:

.net

vb.net

Is There any Function For changing a file extension in .NET? Or i have to rename a file? thanks

For Example I want to rename each file in a directory with ".resxx" extension to .resx. what is the problem with my code?

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
like image 712
Shahin Avatar asked Aug 15 '10 04:08

Shahin


4 Answers

Yes there is: Path.ChangeExtension

In fact the Path class in general has a whole range of useful file/directory name manipulation methods. It's surprising how many developers don't know about it/use it.

like image 101
Ash Avatar answered Oct 06 '22 15:10

Ash


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
like image 31
Mr.N Avatar answered Oct 06 '22 13:10

Mr.N


Changing the file extension of a file is renaming the file.

like image 23
Delan Azabani Avatar answered Oct 06 '22 15:10

Delan Azabani


Solved. Thanks All. :)

Dim [option] As SearchOption = SearchOption.AllDirectories
    [option] = SearchOption.AllDirectories
    Dim files As String()
    files = Directory.GetFiles("C:\New Folder", "*.resxx", [option])
    Dim filepath_new As String
    For Each filepath As String In files
        filepath_new = filepath.Replace(".resxx", ".resx")
        System.IO.File.Move(filepath, filepath_new)
    Next
like image 30
Shahin Avatar answered Oct 06 '22 13:10

Shahin