INI file handling help please
I am writing a program. I have a list box on the left that will load only the section names from the ini file. so say this is my ini file:
[APP1]
location=c:\xxx
[App2]
location=c:\xxx
[prog]
location=c:\xxx
[something]
location=c:\xxx
I need, on Form Load, to open the ini file and grab everything between the "[" and"]" also called sections. so the list box, on form load should look like:
App1
App2
prog
something
I hope this is easy to understand what I am looking for. I already have everything else taken care of. I just need the list to grab all the sections from the ini file and list them. I used to program in VB a lot, but I just got bored and am making a program for myself and a friend, and not this last part has me stumped. Please help! Thanks in Advance!
[861 byte] By [
shudogg] at [2007-11-20 10:35:02]

# 1 Re: INI file handling help please
it's just a text file. load into a buffer, split on lines, and check for [
Option Explicit
Private Sub Form_Load()
Dim x As Integer, st As String
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
Open App.Path & "\to do.txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
' ------ two ways to skin a cat -----
MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
' -----------------
str() = Split(strBuff, vbCrLf)
MsgBox "There are " & UBound(str) + 1 & " lines in the file"
For x = 0 To UBound(str)
st = st & str(x) & vbCrLf & vbCrLf
Next x
MsgBox st
End Sub
# 2 Re: INI file handling help please
Another option is to create a separate section called something like TOC:
[TOC]
Content1=App1
Content2=App2
Content3=prog
Content4=something
In a short loop get the INI setting for "Content" & LoopVar. Stop when nullstring is returned
# 3 Re: INI file handling help please
Thank you both for your prompt replies. I will try dglienna's method though as it looks like what I am looking for, and he/she supplied code :D The problem is, The ini file can be changed and updated by anybody. Basically the ini file has the program name in [Ad-Aware 2007] and the you have like description="blah..." and installfile="somepath/setup.exe"
The program has to search for the [ ] because the program gets its information from the ini file. The ini file could be different, so I cant tell it to grab each specific name. Basically you can add a new entry following the ini standards, and my program will load it into the listbox as well, allowing it to be updateable just by the ini file and not have to re-code and recompile the program every time. I am making an adware/spyware/antivirus/pc tweaking and performance program all off of free software you can find on the net. Kind of like an AIO everything you need free program.
# 4 Re: INI file handling help please
If you have no advance knowledge of the different sections, then by all means use David's suggestion since it has a more universal feel. Should you feel froggy, there is an API that will get the names for you: GetPrivateProfileSectionNames (http://allapi.mentalis.org/apilist/GetPrivateProfileSectionNames.shtml). Example exists at that link.
# 5 Re: INI file handling help please
Ok, well your method still needs a little work. I have had access to a similar code you just gave me, if I knew what to do from there I really wouldn't have posted this message.
I need on form load, to ONLY grab what is in between the two brackets: [Example]
Then it takes the word "Example" and adds it to my listbox. So in the code you provided, it is lacking the ability to split between the brackets to give me the word that is between [ and ]. It needs to grab every instance of this, not just once. So it will need to find the first one, add it to the list box, find the next one, add it to the list box, and so on till the end of the file.
So it is missing either a split or mid of "[" & APPNAME & "]". It is missing List1.AddItem (APPNAME). And I beleive a "while not EOF" command somewhere.
I know the ini file is like so:
[appname]
description=words here
install=setup.exe
but I DO NOT CARE ABOUT anything except the [appname] part. And the app name part is different everytime, so it can not be named specific, it must grab between the two brackets for every time it is in the ini file. So if I have 5 programs, it will add the title of each one of them in list1. I already have the description and everything for the = parts taken care of. All I am waiting on it how to get the appname out of the [ ] and add it to list1.
Thanks for the efforts. Not sounding mean, I am trying to be punctual and help you better understand what I am looking for.
LaVolpe-I followed the link you gave, and I made a new VB app. I created a folder on the desktop and put file.ini in it. I then in the VB app added a list box. I used the code from http://allapi.mentalis.org/apilist/3760A999B066E2CF22C8C0C8C7FEAE03.html which is a page the link you gave me pointed to. I now get Error 14: Out of string space.
This sucks, looks like it may end up working be looking at the code.. but VB wont handle it.
# 6 Re: INI file handling help please
Anybody? I tried the links and get an error with the buffer.
# 7 Re: INI file handling help please
You might want to reconsider the code at the link. I copied your sample INI file from your original posting #1 and saved it to a file on my C: drive called myFile.ini. I then copy & pasted the exact code in the link I provided and ran it. Here is what it printed out to the debug window:
APP1
App2
prog
something
P.S. You may want to upload a copy of your ini file as an attachment.
# 8 Re: INI file handling help please
Using David's suggestion above will work for you too. Pseudo code follows:
Loop until EOF
Read a line, using Line Input
Does line begin with [ and end with ]
if so then extract section name: Mid$(Line,2,Len(Line)-2)
continue loop
# 9 Re: INI file handling help please
Thank you so very much sir. I have had 2 wisdom teeth and a molar pulled yesterday and have been on percoset making it hard to think lol. I have been trying to get this thing going in my spare time. Also with little PC time, I didn't think the project would take this long. I hit a little speed bump with this project, so I decided to ask for help from the pros instead of making it take forever. I think with the help I got from this forum, I will be taking a look around at what else this site has to offer and may be stopping by more often.
I thank you for your warm hospitality and efforts in helping me to the finish line. I appreciate it greatly.
btw-I got it to debug what you got. I then added List1.AddItem Section and it added all the sections only, just what I needed!
OMG I am so happy.. THANKS AGAIN!
PS- Just an FYI for everyone. The reason why I got the error and the app froze is because I didn't have the path located properly. If you enter in C:\myFile.ini it works. If you just say myfile.ini meaning the ini file is in the same folder as the program, it still freezes. you have to use App.Path & "\myFile.ini". That was my problem. I used the code you gave me, but I changed the location to myFile.ini and it froze because it didn't know where to look exactly.
# 10 Re: INI file handling help please
Declare this at the top of the module:
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
And then add this to the module:
Public Function GetINIFile(IniFile As String, ByVal szSection As String, ByVal szField As String)
On Error GoTo Err_GetINIFile
Dim strProcName As String
Dim nRet As Integer
Dim szFileName As String
Dim szBuffer As String
Dim szDefault As String
Dim nTempLength As Integer
strProcName = "GetINIFile"
szDefault = ""
szBuffer = String(1500, " ")
nRet = GetPrivateProfileString(szSection, szField, szDefault, szBuffer, Len(szBuffer), App.path & "\" & IniFile)
If nRet > 0 Then
GetINIFile = Replace(Left(szBuffer, nRet), vbCrLf, "")
Else
GetINIFile = Replace(szDefault, vbCrLf, "")
End If
Exit_GetINIFile:
Exit Function
Err_GetINIFile:
MsgBox Err.Number & Err.Description & vbCrLf & strProcName, vbCritical
GoTo Exit_GetINIFile
End Function
Used thusly:
Dim mTemp as String
mTemp = GetINIFile("file.ini", "Section", "Field")
In this example, the App.Path is inside the function but that's easily changed.
