【发布时间】:2018-10-09 14:34:07
【问题描述】:
我有以下代码来删除文件名中的无效字符。
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Dim fp As String
Private Sub b1_Click(sender As Object, e As EventArgs) Handles b1.Click
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
fp = FolderBrowserDialog1.SelectedPath
t2.Text = fp
End If
End Sub
Private Sub b2_Click(sender As Object, e As EventArgs) Handles b2.Click
Dim files() As FileInfo = New DirectoryInfo(fp).GetFiles("*.*", IO.SearchOption.AllDirectories)
For Each file As FileInfo In files
Dim oldName = file.Name
Dim ons As String = oldName
t1.AppendText(ons + vbNewLine)
Dim newName = Regex.Replace(oldName, "[^0-9a-zA-Z.]", "-")
If oldName <> newName Then
Dim newPath = Path.Combine(file.Directory.FullName, newName)
file.MoveTo(newPath)
End If
Next
End Sub
End Class
这似乎与FileInfo 有问题,不能将其转换为字符串,同时Regex.Replace 也放弃了一些重载问题。
两者,这些都超出了我的理解范围。
【问题讨论】:
-
你能修改上面的行吗
-
查看the properties of FileInfo,它会帮助您找到名称或全名属性。 Regex.Replace 返回值。只是更改字符串不会做任何事情,您需要在之后使用新文件名做一些事情(如renaming the file)。