Author | Message | Time |
---|---|---|
FiReGoD | i need help on write/read ini, i dont understand these functions can someone help me? | January 2, 2003, 10:30 PM |
Grok | QhR LnfuFW? er, i mean What language? | January 2, 2003, 10:32 PM |
Etheran | Try ReadPrivateProfileString and WritePrivateProfileString. | January 3, 2003, 4:08 AM |
Grok | I wrote this class a couple years ago and have used it in all my projects ever since. It is quite useful to me, maybe it will help you. [code] Option Explicit Private Const MAXSECTIONBUFFER = 8192 Private Const MAXENTRYBUFFER = 255 Private Const hNull = 0& Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function GetProfileInt Lib "kernel32" Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Long) As Long Private Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) 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 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 Private Declare Function WriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long Private Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long Private m_INIFileName As String Private m_SectionName As String Private m_KeyName As String Public Property Get FileName() As String FileName = m_INIFileName End Property Public Property Let FileName(ByVal INIFileName As String) m_INIFileName = INIFileName End Property Public Property Get SectionName() As String SectionName = m_SectionName End Property Public Property Let SectionName(ByVal INISectionName As String) m_SectionName = INISectionName End Property Public Property Get KeyName() As String KeyName = m_KeyName End Property Public Property Let KeyName(ByVal INIKey As String) m_KeyName = INIKey End Property Public Property Get IniValue() As String Dim strTemp As String * MAXENTRYBUFFER Dim lret As Long lret = GetPrivateProfileString(m_SectionName, m_KeyName, "", strTemp, Len(strTemp), m_INIFileName) If lret Then IniValue = Left$(strTemp, lret) Else IniValue = "" End If End Property Public Property Let IniValue(ByVal ValueToSet As String) WritePrivateProfileString m_SectionName, m_KeyName, ValueToSet, m_INIFileName End Property Public Sub DeleteSection() WritePrivateProfileString m_SectionName, vbNullString, "", m_INIFileName End Sub 'This Function retrieves all entries in a [Section] of INI file 'entries are null terminated; last entry is double-terminated Public Function GetSectionKeys() As String() Dim strTemp As String * MAXSECTIONBUFFER Dim entries() As String Dim lngListSize As Long lngListSize = GetPrivateProfileString(m_SectionName, 0&, "", strTemp, Len(strTemp), m_INIFileName) entries = NullListToStringArray(strTemp) GetSectionKeys = entries End Function 'receives the section names associated with the named file. The buffer is filled with one or 'more null-terminated strings; the last string is followed by a second null character. Public Function GetSectionNames() As String() Dim strTemp As String * MAXSECTIONBUFFER Dim lngListSize As Long Dim entries() As String Dim lCnt As Long lngListSize = GetPrivateProfileSectionNames(strTemp, Len(strTemp), m_INIFileName) entries = NullListToStringArray(strTemp) GetSectionNames = entries End Function Public Function GetSectionValues() As String() Dim strTemp As String * MAXSECTIONBUFFER Dim lngListSize As Long Dim entries() As String Dim lCnt As Long lngListSize = GetPrivateProfileSection(m_SectionName, strTemp, Len(strTemp), m_INIFileName) GetSectionValues = NullListToStringArray(strTemp) End Function 'retrives the specified entry value from an INI file Public Function GetString(ByVal EntryName As String, ByVal DefaultValue As String) As String Dim strTemp As String * MAXENTRYBUFFER Dim lret As Long lret = GetPrivateProfileString(m_SectionName, EntryName, DefaultValue, strTemp, Len(strTemp), m_INIFileName) If lret Then GetString = Left(strTemp, lret) Else GetString = "" End If End Function Private Function NullListToStringArray(ByVal nList As String) As String() Dim s() As String Dim entry As String Dim lCnt As Long Dim lPos As Long lCnt = 1 ReDim s(1 To lCnt) If Len(nList) = 0 Then s(lCnt) = "" Else lPos = InStr(nList, Chr$(0)) Do While lPos > 0 entry = Left$(nList, lPos - 1) nList = Mid$(nList, lPos + 1) lPos = InStr(nList, Chr$(0)) If Len(entry) > 0 Then ReDim Preserve s(1 To lCnt) s(lCnt) = entry lCnt = lCnt + 1 End If Loop End If NullListToStringArray = s End Function Public Sub WriteSection(ByRef SectionEntries() As String) Dim strTemp As String Dim lCnt As Long Dim lret As Long strTemp = "" For lCnt = LBound(SectionEntries) To UBound(SectionEntries) If Len(SectionEntries(lCnt)) Then If InStr(SectionEntries(lCnt), "=") < 2 Then strTemp = strTemp & SectionEntries(lCnt) & Chr$(0) End If End If Next lCnt strTemp = strTemp & Chr$(0) lret = WritePrivateProfileSection(m_SectionName, strTemp, m_INIFileName) End Sub [/code] Obivously, NullListToStringArray can be replaced by Split, but this code was originally written in VB432, before Split became available. | January 3, 2003, 8:47 AM |
FiReGoD | thanks grok =] | January 3, 2003, 3:23 PM |
St0rm.iD | Write your own. Good learning experience ;) [code] 'Load the file into a string var "filetext" 'The file is in this format: '# this is a comment 'key=value Dim lines as variant Dim i as integer Dim username as string Dim password as string dim key as string dim value as string lines = split(filetext,vbcrlf) For i = lbound(lines) to ubound(lines) if (left(lines(i),1) <> "#") Then Dim args as variant args = split(lines(i), "=") key = lcase(args(lbound(args))) value = args(ubound(args)) if (key = "username") then username = value elseif (key = "password") then password = value end if next i [/code] | January 5, 2003, 2:17 PM |
ILurker | My script is by far the easiest :D Heres the module = http://www.geocities.com/us_lurker/INIModule.zip Enter this code at the top of your form source, [code] Option Explicit Const InI As String = "Main", File As String = "Setup.ini" [/code] Now heres how to read an INI file [code] Dim Account as string Account = ReadINI(InI, "Account", File) textboxname.text = account [/code] Now heres how to write an INI [code] WriteINI InI, "Account", textboxname.Text, File [/code] | February 9, 2003, 11:11 AM |
Grok | The one I wrote reads and writes individual values, and reads or writes whole sections, as well as delete a whole section. | February 9, 2003, 12:02 PM |