2010-08-15 116 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