Solved

INI file issue: Enumerate the Keys of Section.

Posted on 2004-10-07
3
659 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 500 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now