更改文件扩展名VB.NET

时间:2010-08-15 04:47:42

标签: .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

5 个答案:

答案 0 :(得分:11)

是的,有:Path.ChangeExtension

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

答案 1 :(得分: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

答案 2 :(得分:1)

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

答案 3 :(得分: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 :(得分:0)

Sub Button1_Click()
  ' Carl SQL Server Connection
  '
  ' FOR THIS CODE TO WORK
  ' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library

  Dim Cn As ADODB.Connection
  Dim Server_Name As String
  Dim Database_Name As String
  Dim User_ID As String
  Dim Password As String
  Dim SQLStr As String
  Dim rs As ADODB.Recordset
  Set rs = New ADODB.Recordset

  Server_Name = "DAHLIA" ' Enter your server name here
  Database_Name = "TestDB" ' Enter your database name here
  'User_ID = "" ' enter your user ID here
  'Password = "" ' Enter your password here
  SQLStr = "SELECT * FROM [dbo].[TPerson]" ' Enter your SQL here

  Set Cn = New ADODB.Connection
  Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
  ";"

  rs.Open SQLStr, Cn, adOpenStatic
   ' Dump to spreadsheet
  With Worksheets("sheet1").Range("a1:z500") ' Enter your sheet name and range here
      .ClearContents
      .CopyFromRecordset rs
  End With
   '            Tidy up

  rs.Close
  Set rs = Nothing
  MsgBox "Data Exported into SQL."
  Cn.Close
  Set Cn = Nothing
End Sub