Results 1 to 3 of 3

Thread: Classic VB - How can I read/write an .Ini file?

Threaded View

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Classic VB - How can I read/write an .Ini file?

    There are several functions built into the Windows API to help work with Ini files, the code below includes "wrapper" functions which you can use to work with them.

    This code should be pasted into the General Declarations section of your form/module
    VB Code:
    1. 'declarations for working with Ini files
    2. Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
    3.     "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
    4.     ByVal nSize As Long, ByVal lpFileName As String) As Long
    5.  
    6. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    7.     "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    8.     ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, _
    9.     ByVal lpFileName As String) As Long
    10.  
    11. Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias _
    12.     "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, _
    13.     ByVal lpFileName As String) As Long
    14.  
    15. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
    16.     "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    17.     ByVal lpString As Any, ByVal lpFileName As String) As Long
    18.  
    19. '// INI CONTROLLING PROCEDURES
    20. 'reads an Ini string
    21. Public Function ReadIni(Filename As String, Section As String, Key As String) As String
    22. Dim RetVal As String * 255, v As Long
    23.   v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
    24.   ReadIni = Left(RetVal, v - 1)
    25. End Function
    26.  
    27. 'reads an Ini section
    28. Public Function ReadIniSection(Filename As String, Section As String) As String
    29. Dim RetVal As String * 255, v As Long
    30.   v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
    31.   ReadIniSection = Left(RetVal, v - 1)
    32. End Function
    33.  
    34. 'writes an Ini string
    35. Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
    36.   WritePrivateProfileString Section, Key, Value, Filename
    37. End Sub
    38.  
    39. 'writes an Ini section
    40. Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
    41.   WritePrivateProfileSection Section, Value, Filename
    42. End Sub

    Example usage:
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim File As String, OFLen As Double,  Str As String
    4.  
    5.   File = "C:\temp.ini"
    6.   OFLen = FileLen(File)
    7.  
    8.     'write a few example sections:
    9.   WriteIniSection File, "Test1", ""
    10.   WriteIniSection File, "Test2", "Here shoud be found some text"
    11.  
    12.     'write a few ini strings
    13.   WriteIni File, "Test3", "Ini1", "This is ini 1"
    14.   WriteIni File, "Test1", "Ini2", "This is ini 2"
    15.  
    16.     'test message - inform we've written the data
    17.   MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)
    18.  
    19.     'read the ini file
    20.   Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
    21.   Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
    22.   Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
    23.   Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf
    24.  
    25.     'show the data from the file
    26.   MsgBox Str
    27.  
    28. End Sub
    Last edited by si_the_geek; Jul 18th, 2005 at 05:20 PM. Reason: added explanation & tidied up

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