|
-
Dec 2nd, 2002, 05:22 PM
#1
Thread Starter
Hyperactive Member
INI vs. TXT
What is the purpose of an ini file? I know what it does and that programs use it to store settings, but why do they use ini files rather than just regular text files? don't you access the data the same way? reading line by line? what are the pro's and con's of an ini file and is it better? is there a certain format that must be followed? thanks,
- gabe
-
Dec 2nd, 2002, 05:38 PM
#2
Frenzied Member
There is a format that you should follow, but it doesn't matter (I think) if it INI or TXT file. The .INI extention stands for 'Initialize'. That way you could easy find this file. Format for INI file is really simple. Something like this:
[DATA]
Database=C:\mydb.mdb
[SCREEN]
width=100
height=100
If you use this format for your program you don't have to read all file line by line. Use GetPrivateProfileString API function, and you will be able to find any variable just by seaching [SCREEN] header, for example, for 'width' variable. That makes it work very fast.
-
Dec 2nd, 2002, 05:46 PM
#3
Thread Starter
Hyperactive Member
API
You wouldn't happen to know the API string would you? I've never touched API in my life, so I really don't know how to work it.
-
Dec 2nd, 2002, 05:50 PM
#4
Frenzied Member
-
Dec 2nd, 2002, 06:00 PM
#5
Frenzied Member
place this into module:
VB Code:
Public 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) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Function ReadInI(strSectionHeader As String, strVariableName As String, strFileName As String) As String
Dim strReturn As String
'// reads INI file
strReturn = String(255, Chr(0))
ReadInI = Left$(strReturn, _
GetPrivateProfileString(strSectionHeader, ByVal strVariableName, "=", strReturn, Len(strReturn), strFileName))
End Function
Function WriteINI(strSectionHeader As String, strVariableName As String, strValue As String, strFileName As String) As String
'// writes to the INI file
WriteINI = WritePrivateProfileString(strSectionHeader, strVariableName, strValue, strFileName)
End Function
Place on form
VB Code:
'WRITE
Private sub Command1_Click()
Dim ret
ret=WriteINI("SCREEN","width","200","C:\start.ini")
End Sub
'READ
Private Sub Command2_Click()
Dim strWidth
strWidth=ReadINI("SCREEN","width","C:\start.ini")
MsgBox "Width: " & strWidth
End Sub
-
Dec 2nd, 2002, 06:01 PM
#6
Thread Starter
Hyperactive Member
-
Nov 7th, 2006, 11:47 AM
#7
PowerPoster
Re: INI vs. TXT
I've never used INI files either. Why not just store this information in a DB? What is the real advantage of INI files?
-
Nov 7th, 2006, 01:57 PM
#8
Re: INI vs. TXT
 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)
Last edited by schoolbusdriver; Nov 7th, 2006 at 02:22 PM.
-
Nov 7th, 2006, 05:43 PM
#9
PowerPoster
Re: INI vs. TXT
thanks schoolbus....that makes sense now!
-
Nov 7th, 2006, 06:36 PM
#10
Re: INI vs. TXT
blakemckenna,
Think of it this way... If you stored your info in a database, how do you tell your program where the database is to retrieve the settings? That is why you need a different medium to store settings.
-
Nov 7th, 2006, 07:07 PM
#11
Re: INI vs. TXT
Another option of storing data is in the Registry, you can use the GetSetting and SaveSetting functions.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|