04-17-2001, 01:23 PM
This is an open request for you VB gurus and such to provide me with some insight regarding the use of simple INI text files in order to make a VB application more portable and universal from platform to platform.
What I have been told is that INI files are used to store things which can change from system to system - things like directories and paths, different input and output filenames, user preferences, etc.
Could somebody simply include a small INI file to show me what one looks like and then attach some VB code which shows me where (under which control) and how the info in the INI file is extracted and used in the application? Thanks.
04-17-2001, 01:37 PM
Here is a sample of an INI file from an antivirus program:
The various parts are:
1) the section header, the part in square brackets. eg. [TOOLBAR] or [STATUSBAR]
2) the key, the part before the '=' sign. eg. ShowStatusBar
3) the value, the part after the '=' sign. eg 1
You can have keys with the same names under different sections.
To read and write INI files, you can use the API calls GetPrivateProfileString and WritePrivateProfileString. There are others, like GetPrivateProfileInt but the string ones are most useful.
Here are their function declarations:
Declare Function WritePrivateProfileString& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKey As Any, ByVal lpString As String, ByVal lpFileName As String)
Declare Function GetPrivateProfileString& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String)
You can use it like:
Where "STATUSBAR" is the section name (note NO square brackets; the function adds that itself), "ShowStatusBar" is the key and "1" is the value. "inoculan.ini" is the name of the INI file. If it doesn't exist, it will be created.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
04-17-2001, 03:33 PM
as mentioned above by Billsoo you want to use the GetProfileString and WriteProfileString API functions with the INI files. I've placed the two links that open an example to these functions.
vbapi.com has a super reference to most of the api's available and that also have good examples