[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

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?
0
markg1
Asked:
markg1
1 Solution
 
stewfidgeonCommented:
What quotes? Eh?

0
 
markg1Author Commented:
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.
0
 
VBGuruCommented:
use WritePrivateProfileString and GetPrivateProfileString APIs to save/load data with INI files
for more info
http://www.matthart.com/vbhelp/vbmain.htm#INIEX
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
stewfidgeonCommented:
Ok, what code are you using to write to the ini file? Presumably with the WritePrivateProfileString API???
0
 
stewfidgeonCommented:
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.

0
 
markg1Author Commented:
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.
0
 
stewfidgeonCommented:
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.
0
 
markg1Author Commented:
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.
0
 
markg1Author Commented:
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.
0
 
TDragonCommented:
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.

0
 
markg1Author Commented:
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.
0
 
stewfidgeonCommented:
The counter was just an idea so you could put the code into a function. That way you'd not need to change the value of numentries, which is the number of items to add to the list box.

You could change it, so that the combobox name and ini file headers/name were passed to the function aswell. Then you could use it to fill any combobox with any values from an ini file. :-)
0
 
stewfidgeonCommented:
A good modification TDragon. I was getting a lil bit lazy with that single digit thing :)
0
 
markg1Author Commented:
Thanks for your help.  Sorry for being so dense.
0
 
stewfidgeonCommented:
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.

;-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now