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,

How about an OPEN statement followed by READs? I guess I don't understand why an INI file is different from any other file....


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


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:

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:

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:

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

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)
Close #1 ' Close file.
ReadPathFromText = True
Exit Function
ReadPathFromText = False
MsgBox err.description
End Function

Hope this wil help u

This article has been dead for over six months. Start a new discussion instead.