<(UFT) Support Tip> How to read/write values from/to an .ini file?
Recently I came across a situation where the requirement was to read/write values from an initialization (.ini) file. Even though saving settings in .ini files is an old practice, it is still used by many applications, so I’d like to share my findings on how to do it.
Initialization files have an structure like this:
You can find more information about initialization files here.
Using the above structure, let’s say I want to retrieve the value of Key3 under section2 and change the value of Key1 under section1 to “New value”. From UFT, we can use WritePrivateProfileString and GetPrivateProfileString functions from kernel32.dll to accomplish this.
Example on how to retrieve/read the value:
Extern.Declare micLong, "GetPrivateProfileString", "kernel32.dll", "GetPrivateProfileStringA", micString, micString, micString, micString + micByRef, micDWord, micString extern.GetPrivateProfileString("section2", "Key3", "Not found", returnValue, 256, "C:\mySampleINI.ini") msgbox returnValue
Example on how to write a value:
extern.Declare micLong, "WritePrivateProfileString", "kernel32.dll", "WritePrivateProfileString", micString, micString, micString, micString extern.WritePrivateProfileString "section1", "Key2", "New value", "C:\mySampleINI.ini"
If you find this useful, mark Kudos.
Note: The Opinions expressed in my post are mine alone, and do not reflect the opinions of my employer. No warranties express or implied for any answered posted.