How can i get my key's value using a stream reader

[SettingsMB]
Remember Me=1
Auto Log In=1
[DataMB]
Password="Mypassword"
Username="MyEmail"

Recommended Answers

All 3 Replies

Try running this. You may need to add some error checking or tests for blank lines or comments depending on how strictly you control the format of your ini file.

Private Sub btnRead_Click(sender As System.Object, e As System.EventArgs) Handles btnRead.Click

    Dim file As String = "d:\temp\settings.ini"
    Dim sr As New System.IO.StreamReader(file)

    Do Until sr.EndOfStream

        Dim line As String = sr.ReadLine()

        If line.StartsWith("[") Then
            Debug.WriteLine("SECTION: " & line)
        Else
            Dim pair() As String = line.Split("=")
            Debug.WriteLine(Trim(pair(0)) & " is " & Trim(pair(1)))
        End If

    Loop

    sr.Close()

End Sub

A more robust solution would be to create your own Class to read/write ini files. The class could store the entire file as an internal data structure and you could have methods to access/add/delete/read/write. Perhaps a search on Google would turn up such a class already written and debugged.

Here's my example through what ive found on other sites and my own editing and also converted from C# -_-
And was not low class C# XD

Public Sub read()
        Dim GetIniValue As String = ""
        Using MBINILOC As New IO.StreamReader(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Moonbase Hack Utility\MoonbaseSettings.ini")
            Dim CurrentLineInMBINI As String = ""
            Do
                CurrentLineInMBINI = MBINILOC.ReadLine
                If CurrentLineInMBINI.Contains("Password=") Then
                    GetIniValue = CurrentLineInMBINI
                    Exit Do
                End If
            Loop Until CurrentLineInMBINI Is Nothing
        End Using
        MsgBox(GetIniValue.Substring(9))
    End Sub

Do you think there's a better easier solution than this and yours XD
My example is nice and easy but yours C++ all the way XD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.