| | |
How do you read a .ini file?
![]() |
•
•
Join Date: Aug 2006
Posts: 999
Reputation:
Solved Threads: 1
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
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
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.
Hi,
Use This Code
Regards
Veena
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, sSQLRegards
Veena
•
•
Join Date: Sep 2006
Posts: 36
Reputation:
Solved Threads: 1
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:
AppName would be equivalent to the filename of your INI file. For example, let's say I have an application called
Section is just like an INI section:
Key is the value's name, and Setting is the value itself. In
The next method is the one used to retrieve information previously stored in the Registry:
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:
Pretty simple, no?
Now for the last of the builtin Registry methods:
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:
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
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)
SaveSetting "miw", "fixes", "fix0024", "C:\Windows\explorer.exe"
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)
fix(24) = GetSetting("miw", "fixes", "fix0024", "xxxx")
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)
DeleteSetting "miw", "fixes", "fix0024"
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
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
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
Sushanth.CP
Sr. Software Engineer
![]() |
Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: How check Printer before printing?
- Next Thread: problem with mscomm
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






