How to Get all INI file value.

Discussion in 'C#' started by nivedita2104, Sep 2, 2008.

  1. #1
    Hi ALL,

    I have 1 .ini file with some following entries

    abc.ini

    [start]
    file =0

    [end]
    file=10

    I want to value of file both category start and end

    Thanks in advance
    Nivedita
     
    nivedita2104, Sep 2, 2008 IP
  2. Rulzar

    Rulzar Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Option Strict On
    Module INIAccess

    #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


    The code to call this would run along these lines:
    Dim sValue As String
    Dim sPath As String
    sPath = "testing.ini"

    INIWrite(sPath, "Section1", "Key1-1", "Value1-1") ' build INI file
    INIWrite(sPath, "Section1", "Key1-2", "Value1-2")
    INIWrite(sPath, "Section1", "Key1-3", "Value1-3")
    INIWrite(sPath, "Section2", "Key2-1", "Value2-1")
    INIWrite(sPath, "Section2", "Key2-2", "Value2-2")

    sValue = INIRead(sPath, "section2", "key2-1", "Unknown") ' specify all
    MessageBox.Show(sValue, "section2/key2-1/unknown", MessageBoxButtons.OK)

    sValue = INIRead(sPath, "section2", "XYZ", "Unknown") ' specify all
    MessageBox.Show(sValue, "section2/xyz/unknown", MessageBoxButtons.OK)

    sValue = INIRead(sPath, "section2", "XYZ") ' use zero-length string as default
    MessageBox.Show(sValue, "section2/XYZ", MessageBoxButtons.OK)

    sValue = INIRead(sPath, "section1") ' get all keys in section
    sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
    MessageBox.Show(sValue, "section1 pre delete", MessageBoxButtons.OK)

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

    sValue = INIRead(sPath) ' 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)

    INIDelete(sPath, "section1") ' delete section
    sValue = INIRead(sPath) ' get all section names
    sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
    MessageBox.Show(sValue, "All sections post delete", MessageBoxButtons.OK)

    this is vb.net and uses win32 api (old school) but it works great
     
    Rulzar, Sep 4, 2008 IP