Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

INI file issue: Enumerate the Keys of Section.

Posted on 2004-10-07
3
Medium Priority
?
714 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

610 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