Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read & Write INI files

Status
Not open for further replies.

phita

Programmer
Jun 8, 2001
82
KE
Hi,
I need a procedure/class that can read and write INI file entries. Someone please help me with the code to accomplish this.

Thanks,

Phita.
 
THis should help you on the way:

Code:
*-- A class that allows easy read/write access to INI files.
*

DEFINE CLASS cinifile AS csystemsetting


	Width = 152
	Height = 19
	Name = "cinifile"

	*-- The name of the INI file to read/write to. Initialized in Init(), but can be overridden in Set() or Get().
	PROTECTED cinifile


	PROCEDURE set
		LPARAMETERS tcSection, tcEntryName, tuValue, tcINIFile

		LOCAL lcINIFile, ;
		      lcValue

		*-- Make sure we passed in a value to store
		IF PCOUNT() < 3 
		  RETURN .F.
		ENDIF

		*-- Convert value to character if necessary
		IF TYPE(&quot;tuValue&quot;) <> &quot;C&quot;
		  *-- Try to convert the value to character
		  lcValue = STR(tuValue)
		ELSE
		  lcValue = tuValue
		ENDIF

		*-- Use tcINIFile if passed
		IF PCOUNT() < 4 OR TYPE(&quot;tcINIFile&quot;) <> &quot;C&quot;
		  lcINIFile = this.cINIFile
		ELSE
		  lcINIFile = tcINIFile
		ENDIF

		IF UPPER(lcINIFile) = &quot;WIN.INI&quot;
		  lnResult = WriteProfileString(tcSection, tcEntryName, lcValue)
		ELSE
		  lnResult = WritePrivateProfileString(tcSection, tcEntryName, lcValue, lcINIFile)
		ENDIF

		RETURN (lnResult = TRUE)
	ENDPROC


	PROCEDURE get
		LPARAMETERS tcSection, tcEntryName, tuDefault, tcINIFile

		LOCAL lcINIFile, ;
		      lcValue, ;
		      lcBuffer, ;
		      luEntryValue, ;
		      lnNumBytes

		*-- Setup default value 
		IF PCOUNT() >= 3
		  luEntryValue = tuDefault
		ELSE
		  luEntryValue = &quot;&quot;
		ENDIF

		*-- Use tcINIFile if passed
		IF PCOUNT() < 4 OR TYPE(&quot;tcINIFile&quot;) <> &quot;C&quot;
		  lcINIFile = this.cINIFile
		ELSE
		  lcINIFile = tcINIFile
		ENDIF

		lcBuffer = SPACE(255) + CHR(0)

		IF UPPER(lcINIFile) = &quot;WIN.INI&quot;
		  lnNumBytes = GetProfileString(tcSection, tcEntryName, &quot;&quot;, ;
		                              @lcBuffer, LEN(lcBuffer))
		ELSE
		  lnNumBytes = GetPrivateProfileString(tcSection, tcEntryName, &quot;&quot;, ;
		                                    @lcBuffer, LEN(lcBuffer), lcINIFile)
		ENDIF

		IF lnNumBytes = 0
		  RETURN luEntryValue
		ELSE
		  RETURN LEFT(lcBuffer, lnNumBytes)
		ENDIF
	ENDPROC


	PROCEDURE Init
		LPARAMETERS tcINIFile

		*-- DECLARE DLL statements for reading/writing to private INI files
		DECLARE INTEGER GetPrivateProfileString IN Win32API ;
		  String cSection, String cKey, String cDefault, String @cBuffer, ;
		  Integer nBufferSize, String cINIFile

		DECLARE INTEGER WritePrivateProfileString IN Win32API ;
		  String cSection, String cKey, String cValue, String cINIFile

		*-- DECLARE DLL statements for reading/writing to WIN.INI file
		DECLARE INTEGER GetProfileString IN Win32API ;
		  String cSection, String cKey, String cDefault, ;
		  String @cBuffer, Integer nBufferSize

		DECLARE INTEGER WriteProfileString IN Win32API ;
		  String cSection, String cKey, String cValue

		IF PCOUNT() >= 1
		  this.cINIFile = tcINIFile
		ELSE
		  this.cINIFile = &quot;WIN.INI&quot;
		ENDIF
	ENDPROC


ENDDEFINE
Weedz (Edward W.F. Veld)
My private project:Download the CrownBase source code !!
 
Here's a snippet on how to manipulate INI files/values (also derived from Markus Egger's Adv OOP book):

* read the value from the INI file
local lcValue, loINI, lnError
loINI = NewObject(&quot;oldinireg&quot;,home() + &quot;ffc\registry.vcx&quot;)
lnError = loINI.GetINIEntry(@lcValue,&quot;Preferences&quot;,&quot;SysID&quot;,&quot;t:\foxpro\devapps\tricare\main\misc\autobook.ini&quot;)
if lnError = 0 then
wait wind lcValue
else
wait wind &quot;Error!&quot;
endif

* write a new value to that key
loINI.WriteINIEntry(&quot;1234&quot;,&quot;Preferences&quot;,&quot;SYSID&quot;,&quot;t:\foxpro\devapps\tricare\main\misc\autobook.ini&quot;)
if lnError = 0 then
wait wind &quot;Value set successfully!&quot;
else
wait wind &quot;Error writing value!&quot;
endif

HTH,
--Michael

Michael J. Babcock, MCP
Founder, Central PA Visual Foxpro User Group
mbabcock@cpvfug.org
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top