Skip to content

VBMAN.Ini - INI Configuration File Object

Overview

VBMAN.Ini provides convenient INI configuration file read/write functionality, supporting multi-level sections, multi-line text, and automatic path handling.

Core Features

  • Multi-level Sections: Supports [Section] and Key=Value structure
  • Multi-line Text: Supports configuration values containing line breaks
  • Chain Calling: Fluent API design
  • Automatic Encoding: Supports UTF-8 and other encoding formats
  • Path Memory: Can omit path in SaveTo after LoadFrom

Properties

PropertyTypeDescription
RootScripting.DictionaryRoot dictionary object (default member)
LastErrorStringLast error message

Methods

Section

Get or create section (returns dictionary)

vb
Public Property Get Section(ByVal key As String) As Scripting.Dictionary

Example:

vb
' Read section
Dim serverIP As String
serverIP = VBMAN.Ini.Section("Server")("IP")

' Write section
VBMAN.Ini.Section("Server")("IP") = "192.168.1.1"
VBMAN.Ini.Section("Server")("Port") = "8080"

' Chain setting
With VBMAN.Ini.Section("Database")
    .Item("Host") = "localhost"
    .Item("Name") = "mydb"
    .Item("User") = "admin"
End With

MultiLineText

Read/write multi-line text (automatically handles newline escaping)

vb
Public Property Get MultiLineText(ByVal SectionName As String, ByVal KeyName As String) As String
Public Property Let MultiLineText(ByVal SectionName As String, ByVal KeyName As String, ByVal Text As String)

Description: Automatically converts \r\n with actual newline vbCrLf

Example:

vb
' Write multi-line text
Dim notice As String
notice = "System Maintenance Notice" & vbCrLf & _
         "Time: 2024-01-01 00:00" & vbCrLf & _
         "Content: Server Upgrade"

VBMAN.Ini.MultiLineText("Notify", "Content") = notice

' Read multi-line text
Dim content As String
content = VBMAN.Ini.MultiLineText("Notify", "Content")
TextBox1.Text = content

LoadFrom

Load INI from file

vb
Public Function LoadFrom(ByVal Path As String, Optional CharSet As String = "UTF-8") As cIni

Parameters:

  • Path - File path
  • CharSet - Character encoding (default UTF-8)

Example:

vb
' Basic loading
VBMAN.Ini.LoadFrom App.Path & "\\config.ini"

' Chain calling
With VBMAN.Ini
    .LoadFrom App.Path & "\\config.ini"
    Debug.Print .Section("Server")("IP")
End With

SaveTo

Save to file

vb
Public Function SaveTo(Optional ByVal Path As String, Optional CharSet As String = "UTF-8") As Boolean

Description: If Path is omitted, uses the last LoadFrom path

Example:

vb
' Save to specified path
VBMAN.Ini.SaveTo "C:\\config.ini"

' Save to last loaded path (recommended)
VBMAN.Ini.SaveTo

' Complete example
With VBMAN.Ini
    .LoadFrom App.Path & "\\config.ini"
    .Section("Settings")("Theme") = "Dark"
    .SaveTo  ' Automatically save to original path
End With

Password

Set encryption password (chain call)

vb
Public Function Password(ByVal Pwd As String) As cIni

Example:

vb
' Set password (reserved functionality)
VBMAN.Ini.Password("mysecret").LoadFrom "config.ini"

Comprehensive Examples

Example 1: Read/Write Server Configuration

vb
Private Sub SaveServerConfig()
    With VBMAN.Ini
        ' Load or create configuration
        .LoadFrom App.Path & "\\server.ini"
        
        ' Write server configuration
        With .Section("Server")
            .Item("IP") = TextIP.Text
            .Item("Port") = TextPort.Text
            .Item("Timeout") = "30"
        End With
        
        ' Write database configuration
        With .Section("Database")
            .Item("Host") = "localhost"
            .Item("Port") = "3306"
            .Item("Name") = "production"
        End With
        
        ' Save
        If .SaveTo Then
            MsgBox "Configuration saved successfully!"
        Else
            MsgBox "Save failed: " & .LastError
        End If
    End With
End Sub

Private Sub LoadServerConfig()
    With VBMAN.Ini
        .LoadFrom App.Path & "\\server.ini"
        
        TextIP.Text = .Section("Server")("IP")
        TextPort.Text = .Section("Server")("Port")
    End With
End Sub

Example 2: Read Announcement Info

vb
' Usage example from actual project (from cs-auther case)
Private Sub LoadNotice()
    VBMAN.Ini.LoadFrom App.Path & "\\config.ini"
    
    ' Check if announcement exists
    If VBMAN.Ini.Root.Exists("Notify") Then
        TextTitle.Text = VBMAN.Ini("Notify")("Title")
        TextContent.Text = VBMAN.Ini.MultiLineText("Notify", "Content")
        LabelTime.Caption = VBMAN.Ini("Notify")("Time")
    End If
End Sub

Private Sub SaveNotice()
    VBMAN.Ini.Section("Notify")("Title") = TextTitle.Text
    VBMAN.Ini.MultiLineText("Notify", "Content") = TextContent.Text
    VBMAN.Ini.Section("Notify")("Time") = Format(Now, "yyyy-MM-dd hh:mm:ss")
    VBMAN.Ini.SaveTo
End Sub

Example 3: Delete Section

vb
Private Sub ClearNotice()
    VBMAN.Ini.LoadFrom App.Path & "\\config.ini"
    
    ' Delete entire Notify section
    If VBMAN.Ini.Root.Exists("Notify") Then
        VBMAN.Ini.Root.Remove "Notify"
    End If
    
    VBMAN.Ini.SaveTo
End Sub

Example 4: Iterate All Configurations

vb
Private Sub ShowAllConfig()
    VBMAN.Ini.LoadFrom App.Path & "\\config.ini"
    
    Dim sectionKey As Variant
    Dim key As Variant
    
    ' Iterate all sections
    For Each sectionKey In VBMAN.Ini.Root.Keys
        Debug.Print "[" & sectionKey & "]"
        
        ' Iterate all key-value pairs in section
        For Each key In VBMAN.Ini.Root(sectionKey).Keys
            Debug.Print "  " & key & " = " & VBMAN.Ini.Root(sectionKey)(key)
        Next key
    Next sectionKey
End Sub

INI File Format Example

ini
[Server]
IP=192.168.1.100
Port=8080
Timeout=30

[Database]
Host=localhost
Port=3306
Name=mydb
User=admin
Password=secret

[Notify]
Title=System Announcement
Time=2024-01-15 10:30:00
Content=System will be maintained tonight
Estimated time: 2 hours
Please save your work in advance

Best Practices

  1. Use App.Path: Configuration files recommended to be placed in program directory
  2. Check Section Existence: Check if section exists before reading to avoid errors
  3. Use MultiLineText: Use dedicated method to handle newlines for multi-line content
  4. Error Handling: File operations may fail, recommend adding error handling
  5. Save Regularly: Call SaveTo promptly after modifying configuration

Notes

  • Section names and key names are case-sensitive
  • Leading/trailing spaces in values will be trimmed
  • Supports comment lines starting with ; (automatically ignored when reading)
  • Empty lines are ignored

VB6 and LOGO copyright of Microsoft Corporation