Wrox Programmer Forums
|
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2004, 05:39 AM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

thanks:)

 
Old September 3rd, 2004, 06:04 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

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
Loop
Close #100
funcGetPrivateProfileString = sRetValue
End Function

Then you can call the function as below.

strTest = funcGetPrivateProfileString("<key>", "<path to ini file>/sample.ini")
 
Old September 3rd, 2004, 06:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Regards
Owain Williams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Read data from ini file nbryson VB How-To 5 April 2nd, 2015 05:14 AM
Reading an INI file in C# @shish C# 1 April 1st, 2015 04:00 AM
ini file ptop C++ Programming 0 January 30th, 2006 06:29 AM
How to read and update .INI files using Pure C ?? Poonam C++ Programming 1 March 24th, 2005 01:22 AM
INI file access jlick Pro VB 6 4 November 7th, 2003 05:11 AM





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