Results 1 to 5 of 5

Thread: INI Files

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    9

    Unhappy

    Anyone know how to save settings to and wrte from ini files for my application? What are the most convenient/simplest functions?
    blah

  2. #2
    Addicted Member
    Join Date
    Apr 2000
    Location
    Sheffield, England.
    Posts
    136

    Code to read from INI file

    Put this code in a module.

    Code:
    Public Function GetINIValue(ByVal Section As String, SearchFor As String) as String
        Dim Ini As String
        Dim SectStart As Long
        Dim StartPos As Long
        Dim EndPos As Long
        Dim Value As String
        Open "MyINIFile.ini" For Input As #1
            Ini = Input$(LOF(1), 1)
        Close #1
        SectStart = InStr(1, Ini, "[" & Section & "]")
        SectStart = SectStart + Len(Section) + 2
        StartPos = InStr(1, Ini, SearchFor)
        For n = StartPos To Len(Ini)
            If Asc(Mid$(Ini, n, 1)) < 30 Then
            EndPos = n: Exit For
            End If
        Next
        Value = Mid$(Ini, StartPos, EndPos - StartPos)
        Value = Right$(Value, Len(Value) - Len(SearchFor) - 1)
        GetINIValue = Value
    End Function
    Then you just call the function from your main program. The INI file my code is reading from looks like this:-

    [general]
    header=My Program
    [linkoftheday]
    url=www.theonion.com
    caption=The Onion

    "Section" is the bit in square brackets, "SearchFor" is the bit to... erm... search for. So, :-

    Result = GetINIValue("linkoftheday", "url")

    ...would make Result = www.theonion.com

    This code probably can't be modified to write INI settings, and there might be an easier way of reading AND writing INI files. I didn't need to write any settings so this is what I used.


    [Edited by gfk on 06-08-2000 at 10:04 AM]

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb API function

    All you need just to call the following API function:
    • GetPrivateProfileSection
    • GetPrivateProfileString
    • GetPrivateProfileInt
    • WritePrivateProfileSection
    • WritePrivateProfileString


    and hope the following sample code can help you to understand it more clear.

    Code:
    Option Explicit
    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Private nReturn&
    Private Returnstr$
    Private Sub Form_Load()
    'Assume your INI filename is test.ini and have the following structure
    '[MSCOMM]
    'Port=1
    'Setting=2400,e,7,1
    '
    '[SCREEN]
    'Height=768
    'Width=1024
    
    'Set the buffer size for the return string
    Returnstr = String(255, Chr(0))
    'Get the Port setting from "[MSCOMM]" section
    nReturn = GetPrivateProfileString("MSCOMM", "Port", "2", Returnstr, Len(Returnstr), App.Path & "\Test.ini")
    If nReturn <> 0 Then
        'Get the NULL string position
        Debug.Print "Port value is " & Left(Returnstr, InStr(1, Returnstr, Chr(0), vbTextCompare) - 1)
    End If
    
    'Set the buffer size for the return string
    Returnstr = String(255, Chr(0))
    'Get the Port setting from "[MSCOMM]" section
    nReturn = GetPrivateProfileString("MSCOMM", "Setting", "9600,n,8,1", Returnstr, Len(Returnstr), App.Path & "\Test.ini")
    If nReturn <> 0 Then
        'Get the NULL string position
        Debug.Print "Port setting is " & Left(Returnstr, InStr(1, Returnstr, Chr(0), vbTextCompare) - 1)
    End If
    
    'Write the data into Height of the "[SCREEN]" section
    nReturn = WritePrivateProfileString("SCREEN", "Height", "600", App.Path & "\Test.ini")
    
    'Write the data into Width of the "[SCREEN]" section
    nReturn = WritePrivateProfileString("SCREEN", "Width", "800", App.Path & "\Test.ini")
    
    End Sub

  4. #4
    Guest
    Here's an artical on it from Vb Square.

    http://www.vbsquare.com/files/iniclass/

  5. #5
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Or try the demo project from my website...

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