VB.net路径中的非法字符

时间:2017-02-26 23:28:07

标签: vb.net

我正在创建一个应用程序,显示计算机上所有WLAN配置文件的关键。

    '' GET PROFILES
    Dim temp_direct As String = My.Computer.FileSystem.SpecialDirectories.Temp
    Dim cmd As String = "Netsh WLAN show profiles > " & temp_direct & "\profiles.txt"

    Shell("cmd.exe /c " + cmd, AppWinStyle.Hide, True)

    '' SHOW PROFILES
    Dim texto As String = File.ReadAllText(temp_direct & "\profiles.txt").Replace(" ", "")
    Dim pattern As String = "AllUserProfile:(.*)"
    Dim matches As MatchCollection = Regex.Matches(texto, pattern)

    For Each m As Match In matches
        For Each c As Capture In m.Captures
            RichTextBox1.AppendText("SSID: " & m.Groups(1).Value)

            '' SAVE PROFILE WITH CLEAR KEY
            Dim cmd_info As String = "Netsh WLAN show profiles " & m.Groups(1).Value & " key=clear" & " > " & temp_direct & "\" & m.Groups(1).Value & ".txt"
            Shell("cmd.exe /c " + cmd_info, AppWinStyle.Hide, True)

            '' SHOW KEY ON RICHTEXTBOX
            Dim ficheiro As String = m.Groups(1).Value & ".txt"
            Dim pass_file As String = File.ReadAllText(Path.Combine(temp_direct, ficheiro)).Replace(" ", "")
            Dim pattern_pass As String = "KeyContent:(.*)"
            Dim match_key As Match = Regex.Match(pass_file, pattern_pass)

            If match_key.Success Then
                RichTextBox1.AppendText("Key: " & match_key.Groups(1).Value)
            End If

        Next
    Next

我得到一个例外(路径中的非法字符):

Dim pass_file As String = File.ReadAllText(Path.Combine(temp_direct, ficheiro)).Replace(" ", "")

所以我认为'm.Groups(1).Value'的问题出于某种原因它不会被识别为字符串?因为如果我使用

Path.Combine(temp_direct, "NAME.txt")

以这种方式工作,我尝试将匹配更改为“.ToString”,没有分隔文件类型的“.value”等,但它不起作用。我也尝试在论坛上搜索但没有运气。

1 个答案:

答案 0 :(得分:0)

这是有效的,感谢@Slai!

我添加了一个创建文件名的函数

Public Function GetSafeFilename(filename As String) As String

    Return String.Join(".", filename.Split(Path.GetInvalidFileNameChars()))

End Function

最后的代码:

'' GET PROFILES
    Dim temp_direct As String = My.Computer.FileSystem.SpecialDirectories.Temp
    Dim cmd As String = "Netsh WLAN show profiles > " & temp_direct & "\profiles.txt"

    Shell("cmd.exe /c " + cmd, AppWinStyle.Hide, True)

    '' SHOW PROFILES
    Dim texto As String = File.ReadAllText(temp_direct & "\profiles.txt").Replace(" ", "")
    Dim pattern As String = "AllUserProfile:(.*)"
    Dim matches As MatchCollection = Regex.Matches(texto, pattern)

    For Each m As Match In matches
        For Each c As Capture In m.Captures
            RichTextBox1.AppendText("SSID: " & m.Groups(1).Value)

            '' SAVE PROFILE WITH CLEAR KEY
            Dim cmd_info As String = "Netsh WLAN show profiles " & m.Groups(1).Value & " key=clear" & " > " & temp_direct & "\" & m.Groups(1).Value & ".txt"
            Shell("cmd.exe /c " + cmd_info, AppWinStyle.Hide, True)

            '' SHOW KEY ON RICHTEXTBOX
            Dim ficheiro As String = m.Groups(1).Value & "txt"
            Dim pass_file As String = File.ReadAllText(Path.Combine(temp_direct, GetSafeFilename(ficheiro))).Replace(" ", "")
            Dim pattern_pass As String = "KeyContent:(.*)"
            Dim match_key As MatchCollection = Regex.Matches(pass_file, pattern_pass)

            For Each mk As Match In match_key
                For Each ck As Capture In mk.Captures
                    RichTextBox1.AppendText("Key: " & mk.Groups(1).Value)
                Next
            Next
        Next
    Next