Home General VB.NET Write/Read Value from INI file .NET
20 | 05 | 2012
This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade

Write/Read Value from INI file .NET PDF Print E-mail
User Rating: / 1
PoorBest 
Tutorials

 

Add the code below to a Ini file and read and write value using the following strings:

[code]
Sub TestIniFile()
 
Dim objIniFile As New IniFile("c:\Values.ini")
Dim Value As String
 
Value = objIniFile.GetString("PATH", "Systempath", "None") 'Read value from INI file
 
objIniFile.WriteString("PATH", "Systempath ", "C:\") 'write value to INI file
 
End Sub
 
[/code]

 

 

 

[code]
 
 
Class: INIFile
Public Class IniFile
 
        '*************************************
        '**** Code from VisibleVisual.com ****
        '*************************************
    ' API functions
    Private Declare Ansi Function GetPrivateProfileString _
      Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As StringByVal lpDefault As String, _
      ByVal lpReturnedString As System.Text.StringBuilder, _
      ByVal nSize As IntegerByVal lpFileName As String) _
      As Integer
    Private Declare Ansi Function WritePrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As StringByVal lpString As String, _
      ByVal lpFileName As StringAs Integer
    Private Declare Ansi Function GetPrivateProfileInt _
      Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As StringByVal nDefault As Integer, _
      ByVal lpFileName As StringAs Integer
    Private Declare Ansi Function FlushPrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As Integer, _
      ByVal lpKeyName As IntegerByVal lpString As Integer, _
      ByVal lpFileName As StringAs 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 StringByVal Key As StringByVal [Default] As StringAs 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 StringByVal [Default] As IntegerAs 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 StringByVal [Default] As BooleanAs 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 StringByVal 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 StringByVal 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 StringByVal 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]
 

 

 

Add comment


Security code
Refresh

This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade