王咣生 发表于 2005-6-2 21:57:00

[建议] VB读取INI文件很有用, 可以直接读取指定文件行

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _<BR>        (ByVal lpApplicationName As String, _<BR>        ByVal lpKeyName As Any, ByVal lpDefault As String, _<BR>        ByVal lpReturnedString As String, ByVal nSize As Long, _<BR>        ByVal lpFileName As String) As Long<BR>        <BR>       



Public Function ReadIni(ByVal file As String, ByVal section As String, _<BR>                       ByVal key As String, ByVal default As String) As String<BR>                       <BR>                       Dim x As Long<BR>                       Dim sSection As String, sEntry As String, sDefault As String<BR>                       Dim sRetBuf As String, iLenBuf As Integer, sFileName As String


                       'Six arguments<BR>                       sSection$ = section<BR>                       sEntry$ = key<BR>                       sDefault$ = default<BR>                       sRetBuf$ = String$(256, 0) '256 null character<BR>                       iLenBuf% = Len(sRetBuf$)<BR>                       sFileName$ = file<BR>                               <BR>                       'function will return a value of 12 in this case<BR>                       x = GetPrivateProfileString(sSection$, sEntry$, sDefault$, sRetBuf$, iLenBuf%, sFileName$)<BR>                       <BR>                       'sValue$ will contain the string 'PACKARD BELL'<BR>                       sValue$ = Left$(sRetBuf$, x)<BR>                       <BR>                       'return value<BR>                       ReadIni = sValue$<BR>End Function


Sub Test_ReadIni()<BR>        MsgBox ReadIni("d:\test.ini", "TEST", "2", "null")<BR>End Sub


文件d:\test.ini的内容:


<BR>1=hello<BR>2=world<BR>3=!


<BR>

dyheng 发表于 2005-6-2 23:25:00

不过用程序读写INI文件都很麻烦,而且Windows几乎经把ini这种保存信息形式抛弃了.
页: [1]
查看完整版本: [建议] VB读取INI文件很有用, 可以直接读取指定文件行