[VB.NET] Read and Write Inifile (.ini)


Description
     IniFile is a file used to store data that will be used by an application or another application interface. very useful for storing small amount of data such as startup settings and others. IniFile uses almost same as XML. just the use of XML wider than IniFile. 

IniFile also have one advantage that it's light as a text file (. Txt). very suitable for storing small amounts of data settings for lightweight applications. if somewhat complex applications use XML.

Howto
    1. Api Declaration, create a module and write this Windows API :
'wind32 API IniFile
    Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Public Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

  2. Create function to write IniFile:
    'function Write IniFile
    Public Function WriteIni(ByVal sSection As String, ByVal sKeyName As String, ByVal sNewString As String, ByVal sFileName As String) As Integer
        Dim Vb
        Vb = WritePrivateProfileString(sSection, sKeyName, sNewString, sFileName)
    End Function

  3. Create function to read IniFile:
    Public Function ReadIni(ByVal Key As String, ByVal SubKey As String, ByVal strFileName As String) As String
    Dim res As Integer
    Dim data As StringBuilder

    data = New StringBuilder(500)
    res = GetPrivateProfileString(Key, SubKey, "", data, data.Capacity, strFileName)
    Return data.ToString
    End Function

  4. How to call ?
      - to write just call : WriteIni("app_header", "variable", value, "d:\pathinifile.ini")
        the result in Inifile :
        [app_header]
        variable=value
   
       - to read just call : myvar = ReadIni("app_header", "variable", "d:\pathinifile.ini")
         the result : myval = value

   5. a little additional information , if you need to check file existing for example inifile exist or not , you can use this, create function FileExist
    'function check file exist
    Public Function FileExists(ByVal sFileName As String) As Boolean
        Dim intReturn As Integer
        Try
            intReturn = GetAttr(sFileName)
            FileExists = True
        Catch
            Exit Function
            FileExists = False
        End Try
    End Function
   - to use : FileExists("d:\pathinifile.ini"), it will return true if file exist and false if file not found.

works great

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih