Changing the VB.NET File Extension

Is there any function to change the file extension in .NET? Or do I need to rename the file? thanks

As an example, I want to rename each file to a directory with the extension .resxx to .resx. what is the problem with my code?

Dim [option] Like 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 
+4
source share
4 answers

Yes there is: Path.ChangeExtension

In fact, the Path class as a whole has a number of useful file / directory manipulation methods. It is amazing how many developers are not aware of / use it.

+9
source
 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 
+4
source

Changing the file extension of a file renames the file.

+1
source

solved. Thanks to everyone. :)

 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 
+1
source

All Articles