?
Solved

INI files?

Posted on 2005-02-24
3
Medium Priority
?
1,375 Views
Last Modified: 2012-12-03
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?
0
Comment
Question by:broadbent
[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
3 Comments
 
LVL 9

Expert Comment

by:Lacutah
ID: 13395693
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
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
ID: 13396039
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
 
LVL 4

Author Comment

by:broadbent
ID: 13406743
I happen to think that ini files are easier to edit using Notepad say.
I also learnt about Overloads
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

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