【问题标题】:Rename a file extension in VB.NET在 VB.NET 中重命名文件扩展名
【发布时间】:2015-10-17 05:33:51
【问题描述】:

我编写了一个数据排序器,它会为它正在写入的文件写出一个 .build 扩展名,因此当它正在将数据写入特定文件时,没有任何东西会尝试提升它。

最后,我想在程序关闭之前将任何带有 .build 扩展名的内容重命名为 .dat。

 For Each f In outputFiles

        For Each r In TrailerRecords

            ' Set the bill count variable
            If r.VariableField Then
                r.Fields(1) = String.Format("{0:000000}", f.Value)
            End If

            ' Write the trailer record to the output file
            File.AppendAllText(f.Key, r.ToString() & vbLf)
        Next

        ' Rename all.build files to .dat

NEEDS TO GO HERE

        ' Copy each output file to the backup directory
        File.Copy(f.Key, Path.Combine(backupFolder, Path.GetFileName(f.Key)), True)
    Next
End Sub

【问题讨论】:

标签: vb.net


【解决方案1】:

获取文件并重命名:

Dim auxFile as String
Dim files() As String = IO.Directory.GetFiles(myFolderPath, "*.build")

For Each sFile As String In files
    auxFile = IO.Path.GetFileNameWithoutExtension(sFile) & ".dat"
    My.Computer.FileSystem.RenameFile(sFile, auxFile)
Next

您可以改用 File.Copy()True 的覆盖属性。

查看 MSDN 文档:

【讨论】:

  • 非常感谢 SysDragon,这正是我所需要的,不得不尝试一下才能在应用程序中找到正确的位置,但我得到了这些。
猜你喜欢
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 1970-01-01
  • 2018-01-11
  • 2017-07-05
  • 2018-08-06
  • 1970-01-01
相关资源
最近更新 更多