两个.txt之间的项目配对

时间:2017-06-12 20:07:03

标签: vb.net list dictionary

我一直在尝试合并或配对两个文本文件。 一个文件包含User:Key 另一个文件包含Key:Pass 我想创建一个第三个文本文件,其中包含相应的User:Pass对,基于密钥匹配。 以下是我最近尝试过的事情

    Private Sub Rotate()
    Dim Cracked() As String = IO.File.ReadAllLines(TextBox1.Text)
    For Each lineA In Cracked
        TextBox5.Text = lineA
    check()
    Next
End Sub
    Private Sub check()
    Dim toCheck() As String = TextBox5.Text.Split(":")
    Dim tHash As String = toCheck(0)
    Dim tPass As String = toCheck(1)
    Dim lines1() As String = IO.File.ReadAllLines(TextBox2.Text)
    For Each line In lines1
        If lines1.Contains(tHash) Then
            Dim toAdd() As String = line.Split(":")
            Dim uHash As String = toCheck(0)
            Dim uUser As String = toCheck(1)
            ListBox1.Items.Add(uUser + ":" + tPass)
        End If
    Next
End Sub
     Public Sub CopyListBoxToClipboard(ByVal ListBox2 As ListBox)

    Dim buffer As New StringBuilder

    For i As Integer = 0 To ListBox1.Items.Count - 1
        buffer.Append(ListBox1.Items(i).ToString)
        buffer.Append(vbCrLf)
    Next

    My.Computer.Clipboard.SetText(buffer.ToString)

End Sub


Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    CopyListBoxToClipboard(ListBox1)
    End Sub

分隔符发生了变化,但现在:有效。 我尝试拆分和匹配,但文本框5没有旋转或者它在列表中旋转,那就是全部。

1 个答案:

答案 0 :(得分:1)

这样的东西?

Dim KeyPassFile As String = "..."
Dim UserKeyFile As String = "..."
Dim UserPassFile As String = "..."

Dim KeyPass As New Hashtable

' Read Key:Pass file 
For Each Line In IO.File.ReadAllLines(KeyPassFile)
    Dim iStart = Line.IndexOf(":")
    Dim Key = Line.Substring(0, iStart)
    Dim Pass = Line.Substring(iStart + 1)
    KeyPass.Add(Key, Pass)
Next

' Create User:Pass file
Dim OutFile = IO.File.CreateText(UserPassFile)

' Read User:Key file
For Each Line In IO.File.ReadAllLines(UserKeyFile)
    Dim iStart = Line.IndexOf(":")
    Dim User = Line.Substring(0, iStart)
    Dim Key = Line.Substring(iStart + 1)
    If KeyPass.ContainsKey(Key) Then
        ' We have a match for the key, write it to the file
        OutFile.WriteLine(User & ":" & KeyPass(Key))
    End If
Next

OutFile.Close()

这可能不适用于不适合内存的非常大的文件,并且没有重复检查哈希表中的密钥插入,但我会留下一些东西供您使用.. :))

此外,在您的代码中,您阅读TextBox2.Text中指定的文件的次数与TextBox1.Text文件中的行数相同..