Link to home
Start Free TrialLog in
Avatar of markg1
markg1

asked on

Ini files

I am creating an ini for my app but I'm not sure how to take out the quotes.  All ini files that I've seen do not have any.  Any thoughts?
Avatar of stewfidgeon
stewfidgeon

What quotes? Eh?

Avatar of markg1

ASKER

Sorry, in my ini it's like this:
[operating system]
"Windows 95"
"Windows 98"
"Windows NT"

In all the ini files I've seen there are no quotes.
I am using the ini to add items to combo boxes.
use WritePrivateProfileString and GetPrivateProfileString APIs to save/load data with INI files
for more info
http://www.matthart.com/vbhelp/vbmain.htm#INIEX
Ok, what code are you using to write to the ini file? Presumably with the WritePrivateProfileString API???
if you use the code below, you shouldn't get any "" in your file.

Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

That bit goes in a module's declaration section.

This bit goes in your form or function.

lngBytesReturned = WritePrivateProfileString("operating system", "OS", "Windows NT", strTargetPath)

Where lngBytesReturned =1 if the call succeeds, or 0 if it fails. "OS" can be replaced by your own line description, strTargetPath is the location of the ini file, and "Windows NT" is the text you wish to write to the file. You can replace "Windows NT" wit Cstr(var), where var is a variable whose value you wish to write to the file.

Does that answer it?? Lemme know if you need any more help.

Avatar of markg1

ASKER

I am not that familiar with VB and I am just fooling around with how it works.
What I want to do is use an ini file to add those items to a combo box.
That wasnt what you asked. Never mind, heres the solution to your next question.

'This bit is declared in a module

Public 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

Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long



' This bit goes in you form load procedure
Dim x As Integer
Dim numentries As String * 255
Dim Counter As Integer
Dim lngBytesReturned As Long
Dim strIniPath As String
Dim strComboValue As String * 255
Dim strTrimedValue As String

strIniPath = "C:\temp.ini"

lngBytesReturned = GetPrivateProfileString("NumItems", "Numberofitems", "", numentries, Len(numentries), strIniPath)

Counter = CInt(Left(numentries, InStr(numentries, Chr(0)) - 1))

For x = 1 To Counter
    lngBytesReturned = GetPrivateProfileString("OperatingSystem", "Value" & Right$(CStr(x), 1), "", strComboValue, Len(strComboValue), strIniPath)
    strTrimedValue = Left(strComboValue, InStr(strComboValue, Chr(0)) - 1)
    Combo1.AddItem strTrimedValue
Next x


It read the number of entries to put into the combo box from the ini file, and then reads their values and puts them into a combo box.
Avatar of markg1

ASKER

Sorry for the misunderstanding.
Could you explain how the counter works more fully.
I've only been fooling with this for a few months, so I'm not that swift.
Avatar of markg1

ASKER

Sorry for the misunderstanding.
Could you explain how the counter works more fully.
I've only been fooling with this for a few months, so I'm not that swift.
good code stewfidgeon

markg1:
The counter would be used to tell your program how many operating systems there are to load into the combo box.

Here is what your INI would look like to work with my modifications to stewfidgeon's code (listed after this):

[OperatingSystems]
Count     = 3
Value1    = Windows 95
Value2    = Windows 98
Value3    = Windows NT

Here is my version (I'll explain the differences at the bottom):

Public 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

Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long



' This bit goes in you form load procedure
Dim x As Integer
Dim numentries As String * 255
Dim Counter As Integer
Dim lngBytesReturned As Long
Dim strIniPath As String
Dim strComboValue As String * 255
Dim strTrimedValue As String

strIniPath = "C:\temp.ini"

lngBytesReturned = GetPrivateProfileString("OperatingSystems", "Count", "", numentries, Len(numentries), strIniPath)

Counter = CInt(Left(numentries, lngBytesReturned))

For x = 1 To Counter
    lngBytesReturned = GetPrivateProfileString("OperatingSystems", "Value" & trim(CStr(x)), "", strComboValue, Len(strComboValue), strIniPath)
    strTrimedValue = Left(strComboValue, lngBytesReturned)
    Combo1.AddItem strTrimedValue
Next x


MY CHANGES:
Basically the code was all there, just changed it to get the count from the same SECTION of the INI as the items you were reading.  This would allow you to store multiple item lists in the same file and have a separate COUNT in each related SECTION.
If you programmatically, or manually, add an item you would need to increase the COUNT for the related section and add a VALUE# for the new item.  (Which is why I removed the single digit limit from stewfidgeon's code (where is was reading VALUE#))

stewfidgeon had all the information, I just wanted to add a little extra help while explaining the COUNT thing.

Avatar of markg1

ASKER

Thanks, I get it now.
I thought the count was getting created dynamically by going though the ini file, rather than it's value being in the ini file.
ASKER CERTIFIED SOLUTION
Avatar of stewfidgeon
stewfidgeon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A good modification TDragon. I was getting a lil bit lazy with that single digit thing :)
Avatar of markg1

ASKER

Thanks for your help.  Sorry for being so dense.
Nah, everyone has to start somewhere.

You might wanna consider giving away fewer points for easy questions, that way you can ask more of 'em.

;-)