Reading INI files

I tried using search and the help feature in vb 8 but could not find any matches on this subject.
I want to read info from an ini file. Is there an API I could use to get this done Or is there an easy way to do it with vb 8 Thank you.



Answer this question

Reading INI files

  • Jay_187

    How do I update the ini without creating duplicate entries

  • Hidroilio

    You could use the GetPrivateProfileSection API to try and read a section. If it does not exist (i.e. couldn't be read) then create it. However, if it was read, then it exists and you shouldn't create it. I don't have time to write an example out now but I will give you a link to a description of the API I mentioned.

    http://www.allapi.net/apilist/GetPrivateProfileSection.shtml

    Enjoy!


  • venkee

    Thank you for your replies. I did not want to put so much code (that i did not write) into my project. So, I dug around a bit and came up with the following:

    This is for reading only (as that is all I needed to do).

    Declare the following API:
    Private Declare Function GetPrivateProfileStringA Lib "kernel32" ( _
    ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As System.Text.StringBuilder, _
    ByVal nSize As Integer, _
    ByVal lpFileName As String _
    ) As Integer


    Now here is the called function:
    Private Function ReadINI( _
    ByVal Section as String, _
    ByVal Key As String, _
    ByVal File as String _
    ) As String

    Dim strItem As New System.Text.StringBuilder(256)
    GetPrivateProfileStringA(Section, Key, "None", strItem, strItem.Capacity, File)
    Return strItem.ToString
    End Function

    And here is the call:
    ReadINI("Section1", "Key3", "C:\temp\file.ini")

    That will read the value of "Key3" from section "Section1" of the file "C:\temp\file.ini"
    Here is a sample:

    Public Class tempClass
    Private Declare Function GetPrivateProfileStringA Lib "kernel32" ( _
    ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As System.Text.StringBuilder, _
    ByVal nSize As Integer, _
    ByVal lpFileName As String _
    ) As Integer
    Private Function ReadINI( _
    ByVal Section as String, _
    ByVal Key As String, _
    ByVal File as String _
    ) As String
    Dim strItem As New System.Text.StringBuilder(256)
    GetPrivateProfileStringA(Section, Key, "None", strItem, strItem.Capacity, File)
    Return strItem.ToString
    End Function
    TextBox1.Text =
    ReadINI("Section1", "Key3", "C:\temp\file.ini")
    End Class

    With that sample, TextBox1 will have the value of Key3 as it's text.
    By declaring those two functions, the call becomes extremely easy. Enjoy.


  • MichaelLatta

  • JARP2006

    but ignore the comment from norsd, he can't even wirte, so what he thniks doensn't matter

  • Reading INI files