VB.NET Reading Specific Text in a Text File

I want my program to read specific text in a text file. For example, if I have a text file containing the following information.

acc=blah
pass=hello

I want my vb.net application to get that the account variable is blah and the password variable is greeting.

Can someone tell me how to do this?

thanks

+5
source share
7 answers

Here is a little code that after clicking the button will be:

  • take the input file (in this case I created one of them called "test.ini")
  • read values ​​as separate lines
  • , , - "ACC =" "PASS ="

:

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
+5
+3

, - INI... - * PrivateProfile * API , , INI . , .

, INI.

, '=' . , , . XML , .

+2

: XML. .NET Framework XML, , XML, .

, , , , , ( API ).

+1

. "pass =" . VB, :

  • ( , )
  • / ( )
0

, ?

acc, , ( , ) , System.Configuration.ApplicationSettingsBase .

 Private _settings As My.MySettings
   Private _acc as String
   Private _pass as String
   Public ReadOnly Property Settings() As System.Configuration.ApplicationSettingsBase
        Get
            If _settings Is Nothing Then
                _settings = New My.MySettings
            End If
            Return _settings
        End Get
    End Property
    Private Sub SetSettings()
        Settings.SettingsKey = Me.Name
        Dim theSettings As My.MySettings
        theSettings = DirectCast(Settings, My.MySettings)
        theSettings.acc=_acc
        theSettings.pass=_pass        
        Settings.Save()
    End Sub
    Private Sub GetSettings()
        Settings.SettingsKey = Me.Name
        Dim theSettings As My.MySettings
        theSettings = DirectCast(Settings, My.MySettings)
        _acc=theSettings.acc
        _pass=theSettings.pass
    End Sub

GetSettings , , SetSettings

application.exe.config \apps\2.0\etc etc, , clickonce clickonce. : -

<userSettings>
        <MyTestApp.My.MySettings>
            <setting name="acc" serializeAs="String">
                <value>blah</value>
            </setting>
        <setting name="pass" serializeAs="String">
                <value>hello</value>
        </setting>
    </MyTestApp.My.MySettings>
   </userSettings>
0

Writing your own analyzer is not so difficult. I managed to make one for the game (using C #, but VB also has a class Regex). Using this, the variable accin your file will match the character =, and then blahall the past will be the =newline character ( \n). Then go to the next line and repeat.

0
source

All Articles