问题描述:
刚学读init文件,窗体一个Command按钮,点击他时显示ini文件中对应项的值。代码如下:窗体中
Private Sub Command1_Click()
Dim length
Dim lpFileName
Dim Temp As String * 255
lpFileName = App.Path &"\myIni.ini"
length = GetPrivateProfileString("System", "aa", "", Temp, Len(Temp), lpFileName)
MsgBox Left(Temp, length)
End Sub
模块中
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
Public 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
为什么运行时只显示一个OK对话框?
解析:
Option Explicit
Public sIniFileDir As String 'ini文件的路径
Public sNeirong() As String
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
Public Function FlGetUserINI()
Dim lpOut As String * 128
Dim Ret As Long
Dim i As Integer
Dim sUser As String
Dim sKey As String
sIniFileDir = Trim(App.Path)
If Right(sIniFileDir, 1) <>"\" Then sIniFileDir = sIniFileDir &"\"
sIniFileDir = sIniFileDir &"HAS_SIM.INI"
For i = 0 To 1000
sKey = "KEY" &i + 1
Ret = GetPrivateProfileString("keshi", sKey, "", lpOut, 128, sIniFileDir)
sUser = FlLeftB(lpOut, Ret)
If sUser = "" Then
Exit Function
End If
ReDim Preserve sNeirong(i)
sNeirong(i) = sUser
Next
End Function
Public Function FlLeftB(p1 As String, p2 As Long) As String
FlLeftB = StrConv(LeftB(StrConv(p1, vbFromUnicode), p2), vbUnicode)
End Function
使用api函数:给你个模块代码: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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As String, ByVal lpFileName As String) As Long
Public Function GetPPs(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String) As String
Dim Ret As String, NC As Long, ln As Long
ln = &H7FFF
Ret = String(ln, 0)
If Right(App.Path, 1) = "\" Then
NC = GetPrivateProfileString(lpApplicationName, lpKeyName, lpDefault, Ret, ln, App.Path &"SoftConfig.ini")
Else
NC = GetPrivateProfileString(lpApplicationName, lpKeyName, lpDefault, Ret, ln, App.Path &"\SoftConfig.ini")
End If
Ret = Left(Ret, NC)
Dim t As String
t = Ret
t = Replace(t, "{Chr(9)}", Chr(9))
t = Replace(t, "{Chr(10)}", Chr(10))
t = Replace(t, "{Chr(13)}", Chr(13))
t = Replace(t, "{Chr(32)}", Chr(32))
t = Replace(t, Chr(0), "")
GetPPs = t
End Function
Public Function WritePPs(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpKeyVal As String) As String
Dim t As String
t = lpKeyVal
t = Replace(t, Chr(9), "{Chr(9)}")
t = Replace(t, Chr(10), "{Chr(10)}")
t = Replace(t, Chr(13), "{Chr(13)}")
t = Replace(t, Chr(32), "{Chr(32)}")
If Right(App.Path, 1) = "\" Then
WritePrivateProfileString lpApplicationName, lpKeyName, t, App.Path &"SoftConfig.ini"
Else
WritePrivateProfileString lpApplicationName, lpKeyName, t, App.Path &"\SoftConfig.ini"
End If
End Function
给你两个读取ini文件的过程吧'读取配置文件
Public
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
'写入配置文件
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
Public
Function
ReadOption(ByVal
Caption
As
String,
ByVal
Item
As
String,
ByVal
Path
As
String)
As
String
'Caption小节名称;Item项名称;Path配置文件路径
On
Error
Resume
Next
Dim
sBuffer
As
String
sBuffer
=
Space(128)
GetPrivateProfileString
Caption,
Item,
vbNullString,
sBuffer,
128,
Path
ReadOption
=
Left(sBuffer,
InStr(sBuffer,
vbNullChar)
-
1)
End
Function
'写入配置文件
Public
Function
WriteOption(ByVal
Caption
As
String,
ByVal
Item
As
String,
ByVal
Key
As
String,
ByVal
Path
As
String)
'Caption小节名称;Item项名称;Key项值;Path配置文件路径
Dim
sBuffer
As
String
sBuffer
=
Space(128)
sBuffer
=
Key
&
vbNullChar
WriteOption
=
WritePrivateProfileString(Caption,
Item,
sBuffer,
Path)
End
Function
'获得配置文件路径
Public
Function
GetOptPath()
As
String
GetOptPath
=
App.Path
&
"\config.ini"
End
Function
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)