2017-02-26 101 views
-1

我正在製作一個應用程序,顯示計算機上所有WLAN配置文件的關鍵。VB.net路徑中的非法字符

'' 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」,不與分離等文件類型「價值」,但它不工作。我也試圖在論壇上搜索,但沒有運氣。

+0

http://stackoverflow.com/a/23182807/1383168 – Slai

+0

@Slai非常感謝你!它正在工作! –

+0

也有更好的方法來讀取命令輸出http://stackoverflow.com/questions/11504981/reading-and-writing-to-a-command-prompt-console-using-vb-net – Slai

回答

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 
相關問題