
Originally Posted by
blakemckenna
I've never used INI files either. Why not just store this information in a DB? What is the real advantage of INI files?
With ini or other text based files, you don't need a database. You can carry your settings to another PC. Ini files are usually only used to store an app's settings, not user data. An exception would be, say, to store scores for a game.
Of interest to some may be a little used or documented API call - WritePrivateProfileStruct - which lets you write your data in the form of a binary (hex) string to an ini file, and additionally adds a checksum to the end of the string. Its companion - GetPrivateProfileStruct - checks this value for file corruption. The only problem is you can't edit it with notepad. As you might expect from the "...Struct", it's intended for UDTs.
VB Code:
Option Explicit
'Form level code.
'Just put a 3 command button array (cmdPPStruct) on a form.
'Note: YOU should add code to check the return values of the calls for errors :-)
Private Declare Function WritePrivateProfileStruct Lib "kernel32.dll" Alias "WritePrivateProfileStructA" _
(ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
ByVal uSizeStruct As Long, ByVal szFile As String) As Long
Private Declare Function GetPrivateProfileStruct Lib "kernel32.dll" Alias "GetPrivateProfileStructA" _
(ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
ByVal uSizeStruct As Long, ByVal szFile As String) As Long
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" _
(ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Type MyUDT
Username As String * 20
Password As String * 20
End Type
Private Sub cmdPPStruct_Click(index As Integer)
Dim lngRetval As Long
Dim strUDT As String
Dim MyNewUDT As MyUDT
Select Case index
Case 0 'Write it...
MyNewUDT.Username = "MySillyUserName"
MyNewUDT.Password = "MyStrangePassword"
Call WritePrivateProfileStruct("WritePrivateProfileStruct", "Key", _
MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
Case 1 'Read it...
'No need to erase MyNewUDT. It's recreated whenever cmdPPStruct is clicked...
Call GetPrivateProfileStruct("WritePrivateProfileStruct", "Key", _
MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
strUDT = MyNewUDT.Username & vbCrLf & MyNewUDT.Password & vbCrLf
'Display it...
MsgBox strUDT
Case 2 'Delete the section...
Call WritePrivateProfileSection("WritePrivateProfileStruct", vbNullString, App.Path & "\INITest.ini")
End Select
End Sub
Open the file with notepad and you'll see:
VB Code:
[WritePrivateProfileStruct]
Key=4D7953696C6C79557365724E616D6520202020204D79537472616E676550617373776F7264202020E0
Despite googling for these APIs I couldn't find a single example, so just remember - you saw it on VB Forums first
(unless someone knows differently ?)
-----------------------------------------------------------------
BTW, I've seen several posts that say there isn't an API to delete entire sections (including the keys and values) from INI files. Not quite true:
VB Code:
'Delete a value from the specified key.
WritePrivateProfileString(Section, Key, "", Filename)
'Delete a key (and value) from the specified section.
WritePrivateProfileString(Section, Key, vbNullstring, Filename)
'Delete an entire section from an INI file, including all subkeys and values.
WritePrivateProfileString(Section, vbNullString, vbNullString, Filename)
'or
WritePrivateProfileSection(Section, vbNullstring, Filename)