2010-08-15 117 views
4

是否有任何函數在.NET中更改文件擴展名? 或者我必須重命名一個文件? 謝謝更改文件擴展名VB.NET

例如我想重命名.resxx擴展名爲.resx的目錄中的每個文件。我的代碼有什麼問題?

昏暗[選項如SearchOption = SearchOption.AllDirectories [選項] = 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 

回答

9

是的,有:Path.ChangeExtension

其實一般Path class有有用的文件/目錄名操作方法的整個範圍。令人驚訝的是,有多少開發人員不知道/使用它。

1

更改文件重命名的文件的文件擴展名。

1

已解決。 謝謝大家。 :)

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 
4
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 
+0

感謝........................ – 2012-05-29 10:06:16