Simple INI Files and VB Use of Them

Brain_Drain
04-17-2001, 12: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.

BillSoo
04-17-2001, 12:37 PM
Here is a sample of an INI file from an antivirus program:

<PRE>
[SERVICES]
NodeBrowser=0
Databases=0

[DATABASE]
DatabasePath=
DatabaseMappedPath=

[SCANOPTIONS]
BeepOnDetect=1
ScanExeOnly=0
ScanArchives=1
ScanLotusDatabases=1
ScanBoot=1
ScanFiles=1
InfctAct=3
ScanMode=1
AutoDisplay=1
IncrementalScan=0
NotifyOwner=1
NotifySender=0
bNotifyAdministrator=0
bAttachInfoFile=1
ScanAfter=0
ExtName=APP*BIN*COM*DLL*DOT*DOC*DRV*EXE*OVL*OVR*PRG*SYS*VXD*XLT*XLA*XL S*XLW*
SkipExtName=BTR*DBF*SBF*DB*MDB*MDX*NDX*
ArcExtName=ZIP*ARJ*LHA*LZH*MIM*UUE*CAB*
LotusNotesExtName=NSF*NTF*
ReNameExt=AVB
CopyBeforeCure=0
HeuristicScan=0
MacroCureOption=0
DontRenameWhenCureFails=0

[WINDOWSIZE]
Left=88
Top=88
Width=768
Height=558
State=1

[TOOLBAR]
ShowToolbar=1

[STATUSBAR]
ShowStatusbar=1

[QUICKACCESS]
ShowQuickAccess=1


</PRE>

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:

WritePrivateProfileString "STATUSBAR","ShowStatusBar","1","inoculan.ini"

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

usetheforce2
04-17-2001, 02:33 PM
http://vbapi.com/ref/w/writeprofilestring.html

http://vbapi.com/ref/g/getprofilestring.html

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

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum