How do you read a .ini file?

Reply

Join Date: Aug 2006
Posts: 999
Reputation: EnderX is an unknown quantity at this point 
Solved Threads: 1
EnderX EnderX is offline Offline
Posting Shark

How do you read a .ini file?

 
0
  #1
Dec 18th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How do you read a .ini file?

 
0
  #2
Dec 18th, 2006
you can use win32 api functions (scroll down to bottom for GetPrivateProfile??? functions)
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: How do you read a .ini file?

 
0
  #3
Dec 19th, 2006
How about an OPEN statement followed by READs? I guess I don't understand why an INI file is different from any other file....
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: How do you read a .ini file?

 
0
  #4
Dec 19th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 36
Reputation: sendoshin is an unknown quantity at this point 
Solved Threads: 1
sendoshin sendoshin is offline Offline
Light Poster

Re: How do you read a .ini file?

 
0
  #5
Dec 27th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 6
Reputation: sushanth is an unknown quantity at this point 
Solved Threads: 1
sushanth's Avatar
sushanth sushanth is offline Offline
Newbie Poster

Re: How do you read a .ini file?

 
0
  #6
Dec 29th, 2006
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
With Warm Regards,
Sushanth.CP
Sr. Software Engineer
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC