p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Pro VB 6 (http://p2p.wrox.com/forumdisplay.php?f=73)
-   -   How to Read from INI file (http://p2p.wrox.com/showthread.php?t=17857)

alen September 3rd, 2004 05:39 AM

How to Read from INI file
hi everybody,
    i have a vb application that i want to read its settings from a .INI file. i could already retrieve integer values from the file, but i am having trouble retrieving strings, could anyone help me, i really need it.


madhukp September 3rd, 2004 06:04 AM

You can use the following function.

Private Function funcGetPrivateProfileString(ByVal sKey As String, ByVal sFile As String)
Open sFile For Input As #100
Dim sLine As String
Dim strKey As String
Dim strValue As String
Dim sRetValue As String
sRetValue = ""
Dim iPos As Integer
Do While Not EOF(100)
    Line Input #100, sLine
    iPos = InStr(1, sLine, "=")
    If iPos > 0 Then
        strKey = Mid(sLine, 1, iPos - 1)
        strValue = Mid(sLine, iPos + 1, Len(sLine))
        If strKey = sKey Then
            sRetValue = strValue
            Exit Do
        End If
    End If
Close #100
funcGetPrivateProfileString = sRetValue
End Function

Then you can call the function as below.

strTest = funcGetPrivateProfileString("<key>", "<path to ini file>/sample.ini")

owain September 3rd, 2004 06:34 AM

Yea, you could do it that way, however there are several Windows API functions that will do it a lot quicker for you. They are GetPrivateProfileSection, GetPrivateProfileInt and GetPrivateProfileString. They are all relatively easy to use.

Owain Williams

All times are GMT -4. The time now is 02:55 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.