Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 删除文件名中的无效字符_Vb.net_File_Character - Fatal编程技术网

Vb.net 删除文件名中的无效字符

Vb.net 删除文件名中的无效字符,vb.net,file,character,Vb.net,File,Character,我有以下代码来删除文件名中的无效字符 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() = DialogR

我有以下代码来删除文件名中的无效字符

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
Imports System.IO
导入System.Text.RegularExpressions
公开课表格1
将fp设置为字符串
私有子b1\u单击(发送者作为对象,e作为事件参数)处理b1。单击
如果(FolderBrowserDialog1.ShowDialog()=DialogResult.OK),则
fp=FolderBrowserDialog1.SelectedPath
t2.Text=fp
如果结束
端接头
私有子b2_单击(发送者作为对象,e作为事件参数)处理b2。单击
Dim files()作为FileInfo=New DirectoryInfo(fp).GetFiles(“*.*”,IO.SearchOption.AllDirectories)
将每个文件作为文件中的文件信息
Dim oldName=file.Name
Dim on As String=oldName
t1.追加文本(ons+vbNewLine)
Dim newName=Regex.Replace(旧名称“[^0-9a-zA-Z.]”,“-”)
如果oldName newName那么
Dim newPath=Path.Combine(file.Directory.FullName,newName)
file.MoveTo(newPath)
如果结束
下一个
端接头
末级
这似乎与
FileInfo
有关,无法将其转换为字符串,同时
Regex.Replace
也放弃了一些重载问题。
两者都是我无法理解的


filename
不是文件名,而是一个
FileInfo
实例,它有一个
name
属性,您应该改为使用该属性

但是除此之外,
Regex.Replace
返回字符串,但您没有对它做任何操作。那么你想在这里做什么,重命名文件

For Each file As FileInfo In files
    Dim oldName = file.Name
    Dim newName = Regex.Replace(oldName, "[^\w ]", "-")
    If oldName <> newName Then
       Dim newPath = Path.Combine(file.Directory.FullName, newName)
       file.MoveTo(newPath)
    End If
Next
将每个文件作为文件中的文件信息
Dim oldName=file.Name
Dim newName=Regex.Replace(旧名称,[^\w],“-”)
如果oldName newName那么
Dim newPath=Path.Combine(file.Directory.FullName,newName)
file.MoveTo(newPath)
如果结束
下一个

filename
不是文件名,而是一个
FileInfo
实例,它有一个
name
属性,您应该改为使用该属性

但是除此之外,
Regex.Replace
返回字符串,但您没有对它做任何操作。那么你想在这里做什么,重命名文件

For Each file As FileInfo In files
    Dim oldName = file.Name
    Dim newName = Regex.Replace(oldName, "[^\w ]", "-")
    If oldName <> newName Then
       Dim newPath = Path.Combine(file.Directory.FullName, newName)
       file.MoveTo(newPath)
    End If
Next
将每个文件作为文件中的文件信息
Dim oldName=file.Name
Dim newName=Regex.Replace(旧名称,[^\w],“-”)
如果oldName newName那么
Dim newPath=Path.Combine(file.Directory.FullName,newName)
file.MoveTo(newPath)
如果结束
下一个

你能修改上面的行吗?看一看,它会帮助你找到名字或全名​名称属性。返回值。仅仅更改字符串不会起任何作用,您需要在之后使用新文件名(例如)执行一些操作。您可以修改上面的行吗?查看一下它将帮助您找到名称或完整名称​名称属性。返回值。仅更改字符串不会起任何作用,您需要在(如)之后对新文件名执行一些操作。我收到此异常未经处理的错误
System.NotSupportedException:“不支持给定路径的格式”。
来自语法
file.FullName
第行
System.IO.file.Move(file.FullName,newPath)
。你能help@ambrishdhaka:查看编辑,您可以使用。如果不起作用,这里的
newPath
值是多少?使用debugger@ambrishdhaka:很明显你的正则表达式是错的。但这不是问题的一部分,是吗?@ambrishdhaka:FileInfo不是一种“格式”,而是一个类。但是您不需要它,因为FileInfo.MoveTo将新路径导出为字符串。我想你需要看看我上传的最新图片。这绝对不是关于正则表达式的。我得到了这个未经处理的异常错误
System.NotSupportedException:“不支持给定路径的格式”。
来自语法
file.FullName
中的
System.IO.file.Move(file.FullName,newPath)
。你能help@ambrishdhaka:查看编辑,您可以使用。如果不起作用,这里的
newPath
值是多少?使用debugger@ambrishdhaka:很明显你的正则表达式是错的。但这不是问题的一部分,是吗?@ambrishdhaka:FileInfo不是一种“格式”,而是一个类。但是您不需要它,因为FileInfo.MoveTo将新路径导出为字符串。我想你需要看看我上传的最新图片。这绝对不是关于正则表达式的。