VB.NET读取文本文件中的某些文本

lab*_*b12 5 vb.net text file visual-studio-2008

我希望我的程序读取文本文件中的某些文本.例如,如果我有一个包含以下信息的文本文件..

acc=blah
pass=hello
Run Code Online (Sandbox Code Playgroud)

我希望我的vb.net应用程序得到帐户变量等于blah,密码变量等于hello.

谁能告诉我怎么做?

谢谢

Dos*_*tee 5

这是一个快速的一点代码,在您单击按钮后,将:

  1. 获取一个输入文件(在这种情况下,我创建了一个名为"test.ini")
  2. 读取值作为单独的行
  3. 使用正则表达式进行搜索,以查看它是否包含任何"ACC ="或"PASS ="参数
  4. 然后将它们写入控制台

这是代码:

Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim strFile As String = "Test.INI"
    Dim sr As New StreamReader(strFile)
    Dim InputString As String

    While sr.Peek <> -1
        InputString = sr.ReadLine()
        checkIfContains(InputString)
        InputString = String.Empty
    End While
    sr.Close()
End Sub

Private Sub checkIfContains(ByVal inputString As String)
    Dim outputFile As String = "testOutput.txt"
    Dim m As Match
    Dim m2 As Match
    Dim itemPattern As String = "acc=(\S+)"
    Dim itemPattern2 As String = "pass=(\S+)"

    m = Regex.Match(inputString, itemPattern, _
                    RegexOptions.IgnoreCase Or RegexOptions.Compiled)
    m2 = Regex.Match(inputString, itemPattern2, _
                    RegexOptions.IgnoreCase Or RegexOptions.Compiled)
    Do While m.Success
        Console.WriteLine("Found account {0}", _
                          m.Groups(1), m.Groups(1).Index)
        m = m.NextMatch()
    Loop
    Do While m2.Success
        Console.WriteLine("Found password {0}", _
                          m2.Groups(1), m2.Groups(1).Index)
        m2 = m2.NextMatch()
    Loop
End Sub

End Class
Run Code Online (Sandbox Code Playgroud)