944,161 Members | Top Members by Rank

Ad:
Dec 18th, 2006
0

How do you read a .ini file?

Expand Post »
Is there a relatively simple procedure for reading .ini files in Visual Basic? I'm more used to dealing with Delphi, and I have a couple of quick functions (written by another) that deal with .ini handling there, but I've been requested to use VB if at all possible for this particular task. I haven't worked with VB in something like six years, and that was a college 'intro to vb' type course.

All I really need is the ability to read from a .ini file; I'm only going to need a single line to work with, but that line has to be editable by the end user, and the program is supposed to run on startup; this means I can't store it in the executable or require the value to be entered in a data field.

I have looked on the web, but most of what I found seems overly complex, especially relative to the Delphi code variants I'm using now.

Can anyone suggest somewhere else I can look to find what I need?

Thank you for your consideration,
-EnderX
Similar Threads
Reputation Points: 483
Solved Threads: 1
Posting Shark
EnderX is offline Offline
999 posts
since Aug 2006
Dec 18th, 2006
0

Re: How do you read a .ini file?

you can use win32 api functions (scroll down to bottom for GetPrivateProfile??? functions)
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Dec 19th, 2006
0

Re: How do you read a .ini file?

How about an OPEN statement followed by READs? I guess I don't understand why an INI file is different from any other file....
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Dec 19th, 2006
0

Re: How do you read a .ini file?

Hi,

Use This Code

Dim FN As Long
Dim sSQL As String
FN =FreeFile
Open "C:\aaa.ini" For Input As FN
 
Use this code to Read Line :
 
Line Input #FN, sSQL


Regards
Veena
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006
Dec 27th, 2006
0

Re: How do you read a .ini file?

Aside from the handful of Windows API functions (mentioned above) that might serve your INI-handling needs without the need to write your own parsing engine (which is why those functions exist), you do have another option: the Windows Registry. VB6 (at least) provides a few functions for using the registry from your VB programs. These only allow you to play with values in the Software tree of HKCU (and possibly HKLM, though I doubt it), but that's all you really need for what you're describing anyway. Also, what you're describing is the reason VB includes these registry methods in the first place.

First we have the method used to create and save a registry key:
SaveSetting(AppName As String, Section As String, Key As String, Setting As String) I'll try to compare this to a standard INI file.

AppName would be equivalent to the filename of your INI file. For example, let's say I have an application called MakeItWork.exe, with an INI file called miw.ini. To migrate from INI to the Windows Registry, I would simply put miw in place of AppName. Many applications use the value of App.Name instead of a hard-coded value - this is merely personal preference, but can have a few advantages.

Section is just like an INI section: [FIXES] in miw.ini would become fixes as the value of Section. Fairly straightforward.

Key is the value's name, and Setting is the value itself. In [FIXES] I have a line that reads fix0024=C:\Windows\explorer.exe, which indicates that the application has fixed explorer.exe, and that this was the 24th fix the application made. The reason for storing an item like this might be so the program can go back through and make sure the fixes it's already made are still in effect, that the files haven't been rebroken since it was last run. However, this is not important. What is important is how I would place this information in the Windows Registry. Here's the completed call:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. SaveSetting "miw", "fixes", "fix0024", "C:\Windows\explorer.exe"
The next method is the one used to retrieve information previously stored in the Registry: GetSetting(AppName As String, Section As String, Key As String, [Default]) As String Note that this one returns a value. All of the arguments here are the same as in SaveSetting(), except that here Value has been replaced by Default. This is because Value is actually returned by the function. But why the extra arugment then? Well, what if the key you're trying to look up hasn't been saved yet? Default is what the function returns if there isn't a usable Value.

So to get the 24th fix made by MakeItWork, and use the value "xxxx" if no 24th fix was made, I would use the following line:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. fix(24) = GetSetting("miw", "fixes", "fix0024", "xxxx")
Pretty simple, no?

Now for the last of the builtin Registry methods: DeleteSetting(AppName As String, [Section], [Key]) Note that Section and Key are optional; if no Key is provided, the entire Section is removed, and if no Section is provided, everything under AppName is removed. As you likely guessed, if you provide all three, only the specified Key will be removed.

So let us assume that the 24th fix wasn't really a fix at all, since there was nothing to do to begin with. To remove the erroneous entry, I would use the following:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. DeleteSetting "miw", "fixes", "fix0024"
All there is to it.

Now granted, using the Registry instead of .INI files means that your settings take a bit more work to propagate to other machines, and if you have no choice about the use of .INI files the above is all moot. However, it is good practice (in the Windows world) to use the Registry instead of .INI files. The Registry is faster, for one.

Well, that's all I have for the moment. Hope this is useful!

- Sendoshin
Reputation Points: 16
Solved Threads: 1
Light Poster
sendoshin is offline Offline
39 posts
since Sep 2006
Dec 29th, 2006
0

Re: How do you read a .ini file?

Hi,
If you are sure to go with ini file.. you can use below pasted code.. or else you have other option to go for registry.

Public Function ReadPathFromText() As Boolean
On Error GoTo Err_ReadText
Dim TextLine
Dim t1 As Integer
Open App.Path & "\FilePath.ini" For Input As #1 ' Open file.
t1 = 0
Do While Not EOF(1) ' Loop until end of file.
t1 = t1 + 1
Line Input #1, TextLine ' Read line into variable.
If t1 = 2 Then gUserID = TextLine
If t1 = 3 Then gPwd = DeCryptPasswd(TextLine)
Loop
Close #1 ' Close file.
ReadPathFromText = True
Exit Function
Err_ReadText:
ReadPathFromText = False
MsgBox err.description
End Function

Hope this wil help u
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sushanth is offline Offline
6 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: How check Printer before printing?
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: problem with mscomm





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC