2017-02-26 15 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 

私は上の例外(パスに無効な文字)を取得:だから私は、問題は、それはそれはdoesnの何らかの理由で 'm.Groups(1).Valueの' 上だと思います

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

文字列として認識されませんか?私が使用している場合

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

がそのように動作するため、私はなど、区切りファイルの種類で「.VALUE」せずに、「.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 
関連する問題