Search code examples
vb.netini

displaying all key values from an ini file


I have an ini file that I want to get all the dates from. Currently what I have is only getting the first date. I have tried running my function in a loop, but it was still only giving me the 1st date.

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim passfail = GetIniValue("DATE", "self-test-test.ini", "FAIL")
        RichTextBox3.Text = passfail

End Sub

Private Sub RichTextBox3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox3.TextChanged

End Sub

Public Function GetIniValue(key As String, filename As String, Optional defaultValue As String = "") As String
        Dim inifile As New StreamReader(filename)
        Dim LineData As String
        Dim textdata() As String
        Dim keydata As String = defaultValue
        Dim done As Boolean = False

        If File.Exists(filename) Then
            Do While Not done
                If inifile.Peek() >= 0 Then
                    LineData = inifile.ReadLine()
                    textdata = Split(LineData, "=")
                    If Trim(textdata(0)) = key Then
                        If UBound(textdata) > 0 Then
                            keydata = Trim(textdata(1))
                        End If
                        done = True
                    End If
                Else
                    done = True
                End If
            Loop
            inifile.Close()
        End If
        GetIniValue = keydata
    End Function
End Class
> ini file self-test-test.ini
[20E2SN5]
DATE=05/01/2019,15:12:12
STATUS=PASS
[20R5SN9]
DATE=06/21/2019,15:13:13
STATUS=PASS

this was one of the loops I have tried but was still only giving me the first date.

 ``` Dim passfail = GetIniValue("DATE", "self-test-test.ini", "FAIL")

    For Each n In passfail
        RichTextBox3.Text += n.ToString
    Next

currently I am getting a return of
05/01/2019,15:12:12

but would like to receive a return of 

05/01/2019,15:12:12
06/21/2019,15:13:13


Solution

  • Use Windows API GetPrivateProfileSectionNames to get all the section names, then use GetPrivateProfileString to get the value for the "DATE" key.

    You can use the helper functions (GetIniSectionNames and GetIniValue) below.

      Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (lpszReturnBuffer As IntPtr, nSize As UInteger, lpFileName As String) As UInteger
      Private Sub GetIniSectionNames(iniFilename As String, ByRef sectionNames() As String)
        Const MAX_BUFFER As UInteger = 32767
        Dim pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER)
        Dim bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFilename)
        If bytesReturned = 0 Then sectionNames = Nothing
        Dim local As String = Marshal.PtrToStringAnsi(pReturnedString, bytesReturned).ToString
        Marshal.FreeCoTaskMem(pReturnedString)
        sectionNames = local.Substring(0, local.Length - 1).Split(vbNullChar)
      End Sub
    
      Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (lpSectionName As String, lpKeyName As String, lpdefaults As String, lpReturnedstring As String, nSize As Integer, lpFileName As String) As Integer
      Private Sub GetIniValue(iniFilename As String, iniSection As String, iniKey As String, defaultValue As String, ByRef exists As Boolean, ByRef value As String)
        Dim lngResult As Integer
        Dim buffer As String = New String(" ", 255)
        Dim bufferSize As Integer = buffer.Length
        lngResult = GetPrivateProfileString(iniSection, iniKey, defaultValue, buffer, bufferSize, iniFilename)
        If lngResult > 0 Then
          exists = True
        Else
          exists = False
        End If
        value = buffer.Substring(0, lngResult)
      End Sub