?
Solved

INI file issue: Enumerate the Keys of Section.

Posted on 2004-10-07
3
Medium Priority
?
709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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