CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2002
    Location
    London
    Posts
    10

    Question Read/Write to .ini files in vb.net

    I'm trying to read and write .ini files from a VB.NET DLL. I've copied my VB6 code (declarations for GetPrivateProfileString, WritePrivateProfileString, etc.) and they don't appear to work. Instead of getting the contents of my ini file I get a 0 and my returned number of bytes is 5324320632995841.

    Does anybody have any ideas? Do I need to change my API calls to work under .NET?

    Cheers

  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878
    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", "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)
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878
    Check out API. They have to be changed
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  4. #4
    Join Date
    May 2002
    Location
    London
    Posts
    10

    Cheers

    Worked like a charm )

  5. #5
    Join Date
    Mar 2006
    Posts
    1

    Re: Read/Write to .ini files in vb.net

    I have copied and pasted this code into by VB.NET project, and .NET does not seem to have a problem with the code, however nothing is being written to my INI file. I have created the INI file by using a File.Create("ExportSettings.ini"), which creates a file in the same directory as my executable, but when I try to write values to the INI file, nothing shows up. Here is my code:

    Dim sINIpath as String
    sINIpath = "ExportSettings.ini"

    If File.Exists(sINIpath) = False then
    File.Create(sINIpath)
    modINIAccess.INIWrite(sINIpath, "sde", "connectstring", "1")
    End If

    Any insight would be greatly appreciated!

  6. #6
    Join Date
    Mar 2006
    Posts
    16

    Arrow Re: Read/Write to .ini files in vb.net

    See this link for the Dot net way to do this
    http://msdn.microsoft.com/vbasic/att...o/default.aspx

  7. #7
    Join Date
    Jan 2006
    Posts
    293

    Re: Read/Write to .ini files in vb.net

    Well I notice that you are not specifying the full path to the file... you have no directory listed, just the filename...
    Code:
    sINIpath = "ExportSettings.ini"
    That needs to be the full path... if you are trying to read or write it to the application folder, change it to
    Code:
    sINIpath = Application.StartupPath & "\ExportSettings.ini"

  8. #8
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Read/Write to .ini files in vb.net

    There is a free library called Nini that I use to read and write ini files (and other configuration formats also):
    http://nini.sourceforge.net/

    It's great. It provides a unified interface to all configuration formats (ini, xml, command line arguments and registry). No need to use those ugly API commands.

  9. #9
    Join Date
    Jan 2006
    Posts
    293

    Re: Read/Write to .ini files in vb.net

    Of course, if you want to get very basic with this, you just create a simple text file, each line you specify a name, a seperator, and a value... then just use a streamreader, reading each line in, splitting on the seperator, checking the name, if it is the name you want, then use its value...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured