INI files?

I've started re-writing a programme in VB.Net from VB6, where I used ini files to store data, which the user can simply edit.
I am using Windows API calls GetPrivateProfileSection etc. in Windows XP.
I am having a lot of difficulty returning the info - so can anyone help?
LVL 4
broadbentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LacutahCommented:
In .NET the prefered way is to store the values in .Config files, they are essentially XML files with configuration information for your application similar to ini files.

The config file should be named after the executable file.   For example:
My Executable is called:   MyTestProgram.exe
My Config File will be:  MyTestProgram.exe.config

You can have Visual Studio create an empty config file for you by adding a "Application Configuration File" to your project.

To retrieve a value from the Config file, use something like the following which gets a value called "DirToSaveTo" from the file and puts it in a string called "s":
        Dim apr As New System.Configuration.AppSettingsReader()
        Dim s As String = CStr(apr.GetValue("DirToSaveTo", GetType(String)))
0
iboutchkineCommented:
I'd rather use XML files, but if you want to use INI here is an example


The following module demonstrates how to access INI files from VB.Net. It also illustrates the use of
function overloading to allow different functionality from the same call by using different parameters:

    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", "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)

        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", "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)


        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)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
broadbentAuthor Commented:
I happen to think that ini files are easier to edit using Notepad say.
I also learnt about Overloads
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.