?
Solved

INI file issue: Enumerate the Keys of Section.

Posted on 2004-10-07
3
Medium Priority
?
718 Views
Last Modified: 2007-12-19
Dear experts,
I want to enumerate all the Keys (key/value pair) under the sections in the INI file. Please consider the example.

-------------- MyIni.INI <= My ini file name --------------

[SERVER]
Addr=123.456.777.10
Port=8888

[LOG]
Path=C:\My_Space\Svr1.0\Log\
Filename=MyLog.log

[UI]
Mode=True

-------------- INI file ends here --------------

I need a function something like this:
Private Sub GetSectionKeys(ByVal sSection As String)
  ' Logic goes here
End Sub

If I call this function like GetSectionKeys("LOG") It should enumerate
Path=C:\My_Space\Svr1.0\Log\
Filename=MyLog.log

Thanks.
NOTE: I know that I can use XML while can be more easy but I am bound to stick using INI files.

PLEASE HAVE A LOOK OF THIS QUESTION ALSO AND EARN MORE POINTS.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21157927.html

rEGARDS.
0
Comment
Question by:KarcOrigin
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 2000 total points
ID: 12247843
'Module
Module ModuleINI
#Region "API Calls"
    ' standard API declarations for INI access
    ' changing only "As Long" to "As Int32" (As Integer would work also)
    Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
      Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpString As String, _
      ByVal lpFileName As String) As Int32

    Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
      Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpDefault As String, _
      ByVal lpReturnedString As String, ByVal nSize As Int32, _
      ByVal lpFileName As String) As Int32
#End Region

    Public Overloads Function INIRead(ByVal INIPath As String, _
      ByVal SectionName As String, ByVal KeyName As String, _
      ByVal DefaultValue As String) As String
        ' primary version of call gets single value given all parameters
        Dim n As Int32
        Dim sData As String
        sData = space$(1024) ' allocate some room
        n = GetPrivateProfileString(SectionName, KeyName, DefaultValue, _
          sData, sData.Length, INIPath)
        If n > 0 Then ' return whatever it gave us
            INIRead = sdata.Substring(0, n)
        Else
            iniread = ""
        End If
    End Function

#Region "INIRead Overloads"
    Public Overloads Function INIRead(ByVal INIPath As String, _
      ByVal SectionName As String, ByVal KeyName As String) As String
        ' overload 1 assumes zero-length default
        Return INIRead(inipath, sectionname, KeyName, "")
    End Function

    Public Overloads Function INIRead(ByVal INIPath As String, _
      ByVal SectionName As String) As String
        ' overload 2 returns all keys in a given section of the given file
        Return INIRead(inipath, sectionname, Nothing, "")
    End Function

    Public Overloads Function INIRead(ByVal INIPath As String) As String
        ' overload 3 returns all section names given just path
        Return INIRead(inipath, Nothing, Nothing, "")
    End Function
#End Region

    Public Sub INIWrite(ByVal INIPath As String, ByVal SectionName As String, _
      ByVal KeyName As String, ByVal TheValue As String)
        Call WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)
    End Sub

    Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String, _
      ByVal KeyName As String) ' delete single line from section
        Call WritePrivateProfileString(SectionName, KeyName, Nothing, INIPath)
    End Sub

    Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String)
        ' delete section from INI file
        Call WritePrivateProfileString(SectionName, Nothing, Nothing, INIPath)
    End Sub

End Module

'FORM
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region

    Dim fsINIPath As String = Application.StartupPath & "\Test.ini"

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        Dim sValue As String
'GROUP is a section name
        INIWrite(fsINIPath, "GROUP", "NewKey1", "Value1") ' build INI file
        INIWrite(fsINIPath, "Section1", "Key1-1", "Value1-1") ' build INI file
        INIWrite(fsINIPath, "Section2", "Key2-1", "Value2-1")
        MsgBox("done")
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        'reads all keys in section
        Dim sValue As String
        Dim arr() As String
        Dim i As Integer

        sValue = INIRead(fsINIPath, "GROUP") ' get all keys in section
        sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
        MessageBox.Show(sValue, "All the Keys in section GROUP", MessageBoxButtons.OK)
        arr = Split(sValue, "|")
        Dim msg As String
        For i = 0 To UBound(arr)
            msg &= arr(i) & vbCrLf
        Next
        MessageBox.Show(msg)
    End Sub

    Private Sub btnReadKeyInSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadKeyInSection.Click
        Dim sValue As String
        'if key is unknown return the admin address (deal@...)
        sValue = INIRead(fsINIPath, "GROUP", "Iouri", "bla") ' specify all
        MessageBox.Show(sValue, "Key does not exist return default", MessageBoxButtons.OK)
    End Sub
    Private Sub btnReadSections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadSections.Click
        Dim sValue As String

        sValue = INIRead(fsINIPath) ' get all section names
        sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
        MessageBox.Show(sValue, "All sections pre delete", MessageBoxButtons.OK)
    End Sub

    Private Sub btnDeleteSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteSection.Click
        Dim sValue As String

        INIDelete(fsINIPath, "section1") ' delete section
        sValue = INIRead(fsINIPath) ' get all section names
        sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
        MessageBox.Show(sValue, "section1 deleted", MessageBoxButtons.OK)
    End Sub

    Private Sub btnDeleteKeyInSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteKeyInSection.Click
        Dim sValue As String

        INIDelete(fsINIPath, "section1", "key1-2") ' delete middle entry in section 1
        sValue = INIRead(fsINIPath, "section1") ' get all keys in section again
        sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
        MessageBox.Show(sValue, "key1-2 in section1 deleted", MessageBoxButtons.OK)
    End Sub

End Class

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12249271
Iouri, the Code Man, comes through again :)

Bob
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12249387
That is our life Bob, Isn't it?  :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question