VB.NET - How to read INI file? - Asked By Ralph Acido on 28-Jun-06 11:19 PM

How read and write INI file values in VB .net and display it in a form?

Here's the content of my INI file:

[general]
Provider=SQLOLEDB
Data Source=Cornelius
Initial Catalog=Academics
User ID=" "
Password=" "
Integrated Security=SSPI


Thanks :)

Use filestream - Asked By Shallu Gupta on 28-Jun-06 11:38 PM

Dim fs=New FileStream("<filename>",FileMode.Open,FileAccess.Read)
Dim d as new StreamReader(fs)
d.BaseStream.Seek(0,SeekOrigin.Begin)
while d.peek()>-1
Textbox1.Text &= d.readLine()
End while
d.close()

you can also split individual line based on "=" as a delimiter

Code - Asked By J S on 29-Jun-06 02:00 AM

Microsoft wants people to use the registry instead of INI files.

But Here's a class for reading ini files (not mine, found it somewhere) - at bottom

Use it like this
[CODE]
        objIniFile = New IniFile("C:\AscentAutoImportXMLGenerator\setup.ini")

objIniFile.GetString("setup", "FileDirectory", "")

where ini file looks like

[setup]
FileDirectory=whatever

'Utility Class for Reading INI Files
Public Class IniFile
    ' API functions
    Private Declare Ansi Function GetPrivateProfileString _
      Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpDefault As String, _
      ByVal lpReturnedString As System.Text.StringBuilder, _
      ByVal nSize As Integer, ByVal lpFileName As String) _
      As Integer
    Private Declare Ansi Function WritePrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpString As String, _
      ByVal lpFileName As String) As Integer
    Private Declare Ansi Function GetPrivateProfileInt _
      Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal nDefault As Integer, _
      ByVal lpFileName As String) As Integer
    Private Declare Ansi Function FlushPrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As Integer, _
      ByVal lpKeyName As Integer, ByVal lpString As Integer, _
      ByVal lpFileName As String) As Integer
    Dim strFilename As String

    ' Constructor, accepting a filename
    Public Sub New(ByVal Filename As String)
        strFilename = Filename
    End Sub

    ' Read-only filename property
    ReadOnly Property FileName() As String
        Get
            Return strFilename
        End Get
    End Property

    Public Function GetString(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As String) As String
        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, _
           [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then GetString = _
           Left(objResult.ToString, intCharCount)
    End Function

    Public Function GetInteger(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As Integer) As Integer
        ' Returns an integer from your INI file
        Return GetPrivateProfileInt(Section, Key, _
           [Default], strFilename)
    End Function

    Public Function GetBoolean(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As Boolean) As Boolean
        ' Returns a boolean from your INI file
        Return (GetPrivateProfileInt(Section, Key, _
           CInt([Default]), strFilename) = 1)
    End Function

    Public Sub WriteString(ByVal Section As String, _
      ByVal Key As String, ByVal Value As String)
        ' Writes a string to your INI file
        WritePrivateProfileString(Section, Key, Value, strFilename)
        Flush()
    End Sub

    Public Sub WriteInteger(ByVal Section As String, _
      ByVal Key As String, ByVal Value As Integer)
        ' Writes an integer to your INI file
        WriteString(Section, Key, CStr(Value))
        Flush()
    End Sub

    Public Sub WriteBoolean(ByVal Section As String, _
      ByVal Key As String, ByVal Value As Boolean)
        ' Writes a boolean to your INI file
        WriteString(Section, Key, CStr(CInt(Value)))
        Flush()
    End Sub

    Private Sub Flush()
        ' Stores all the cached changes to your INI file
        FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub

End Class[/CODE]

re - Asked By Rupali Randhave Kolhe on 29-Jun-06 07:36 AM

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) 
**************************** 
For more info refer links 
http://www.codeproject.com/vb/net/VbNetClassIniFile.asp