|
-
Sep 16th, 2000, 01:09 PM
#1
Thread Starter
Hyperactive Member
How the heck do I use this ?
Code:
Public Function INIGetAllSettings(SFilename As String, ByVal sSection As String) As Variant
' Returns an variant array of all keys(0) and values(1) same as GetAllSettings
' This is the complicated one. It reads all of the Key Names into a temporary array
' then after the array has been read it will crate another array. The new array is
' 2 dimensional, the first dimension is the pair number. The second dimension
' is 0 for the keyname, 1 for the value.
#If Win32 Then
Dim xRet As Long
#Else
Dim xRet As Integer
#End If
Dim sReturnStr As String
Dim nStringLen As Integer
Dim nEndOfKey As Integer
Dim nNumKeys As Integer
Dim arrValues() As Variant
nStringLen = 5000 ' Must be big enough to hold all keys
sReturnStr = String(nStringLen, Chr$(0))
nNumKeys = -1
xRet = GetPrivateProfileString(sSection, 0&, "", sReturnStr, nStringLen, SFilename)
' Parse the string, and add the elements to the array
Do While (InStr(sReturnStr, Chr$(0)) > 1)
' Get each key in the section
nEndOfKey = InStr(sReturnStr, Chr$(0))
nNumKeys = nNumKeys + 1
ReDim Preserve arrValues(nNumKeys)
arrValues(nNumKeys) = Left$(sReturnStr, nEndOfKey - 1)
sReturnStr = Mid(sReturnStr, nEndOfKey + 1)
Loop
Debug.Print INIGetAllSettings
If nNumKeys = -1 Then
' if no keys return an empty variant
INIGetAllSettings = Empty
Else
' Get the values for each key and return that, to maintain compliance with
' GetAllSettings
ReDim arrFullArray(0 To nNumKeys, 0 To 1) As Variant
For nNumKeys = LBound(arrValues) To UBound(arrValues)
arrFullArray(nNumKeys, 0) = arrValues(nNumKeys)
arrFullArray(nNumKeys, 1) = INIGetSetting(SFilename, sSection, arrValues(nNumKeys))
Next nNumKeys
INIGetAllSettings = arrFullArray
End If
End Function
I know I have to give it a file location and name but How does it return ? An Example would be beautiful !
Visual Basic 6 SP4 on win98se
QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!
-
Sep 16th, 2000, 01:31 PM
#2
Uses this api function:
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
And how to use it:
Code:
Text1.text = INIGetAllSettings("C:\INIFile.ini", "INI File")
INI File would probably look something like this:
[INI File]
INIFile=keyvalue
And it loads the file into a variable or textbox as an array.
But here is another way:
Code:
Public Function readini(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let readini$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Text1.text = readini("INI File", "INI File", "C:\INIFile.ini")
And that'll look the same, but that'll state the section, key, and file.
-
Sep 16th, 2000, 02:02 PM
#3
Thread Starter
Hyperactive Member
But what if I don't know all the sections ?
I thought I Could use it to get all the section names like
[return this]
not this
or this
[return this also]
but not this
is that possible ? or do I have to parse the whole file with
instr( ) ?
[Edited by PRIVATE1 on 09-16-2000 at 03:16 PM]
Visual Basic 6 SP4 on win98se
QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!
-
Sep 16th, 2000, 02:23 PM
#4
I don't think there is a way. You must know the sections in order to retrieve the key value.
Unless the INIGetAllSettings works, I think that will get everything in the ini.
' Get the values for each key and return that, to maintain compliance with
' GetAllSettings
Maybe you can, but I don't know if it works or not, code doesn't work for me.
-
Sep 16th, 2000, 03:31 PM
#5
Thread Starter
Hyperactive Member
If anyone want it this is what I came up with
Code:
Function Get_Keys_In_Ini_File(ByVal strFilePath As String) As String
'Added 9/16/00 by Private
'
'Usage this$ = Get_Keys_In_Ini_File (c:\test.ini) returns a delimited string
'
'
Dim Keys As String
Dim fileFile As Integer
Dim KeyCount As Integer
Keys = ""
KeyCount = 0
'open file
fileFile = FreeFile
If (FileCheck(strFilePath)) Then
Open strFilePath For Input As fileFile
Else
'file doesn't exist
MsgBox "File: " & strFilePath & " doesn't excist . Preprocessing is being aborted ."
Count_Lines_In_File = -1
Exit Function
End If
'loop through file
Dim strBuffer As String
Do While Not EOF(fileFile)
'read line
Input #fileFile, strBuffer
'update count
If InStr(1, strBuffer, "[") And InStr(1, strBuffer, "]") Then _
KeyCount = KeyCount + 1
If InStr(1, strBuffer, "[") And InStr(1, strBuffer, "]") Then _
Keys = Keys & ":" & strBuffer
Loop
'close file
Close fileFile
'return value
Get_Keys_In_Ini_File = Keys
End Function
Usage:
Code:
Private Sub Command2_Click()
MsgBox Get_Keys_In_Ini_File(App.Path & "\" & ConfigurationData.Username & ".ini")
End Sub
then just parse your string
Visual Basic 6 SP4 on win98se
QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!
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
|