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
参数说明lpApplicationName 上面例子中的 项名称lpKeyName上面例子中的 键名称 (也就是你说的a,b,c)lpDefault取值时的默认值(如果不存在该键时按该值返回)lpReturnedString 取值返回到的变量名(字串型)nSize 取值长度大小lpFileNameINI文件名 调用举例dim a as stringa=space$(30) '取30长度GetPrivateProfileString 项名称,"a","11",a,30,"C:\abcd\1.ini"
Private Sub command1_click() Open "F:\360data\重要数据\桌面\23.ini" For Output As #1 Print #1, "aa,1=" &Text2.Text Close #1 End Sub 有什么不懂加Q 734856946分二步:一、新建一个模块,复制下面代码:
Option Explicit
Declare Function GetPrivateProfileStringByKeyName&Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey$, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Declare Function WritePrivateProfileStringByKeyName&Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String)
Private rtn As String
Private success As String
Function GetPrivateStringValue(section$, Key$, File$) As String
Dim KeyValue$
Dim characters As Long
If Dir(File$) = "" Then Exit Function
KeyValue$ = String$(FileLen(File$), 0)
characters = GetPrivateProfileStringByKeyName(section$, Key$, "", KeyValue$, Len(KeyValue$) - 1, File$)
If characters >1 Then
KeyValue$ = StrConv(LeftB(StrConv(KeyValue$, vbFromUnicode), characters), vbUnicode)
End If
GetPrivateStringValue = Replace(KeyValue$, Chr(0), "")
End Function
二、调用举例:
Private Sub Command1_Click()
Dim inifile As String, s As String
inifile = App.Path &"\2.ini" 'ini文件名,请根据实际修改
s = GetPrivateStringValue("option", "url", inifile) '读取ini文件中option项下的url值
if s="123" then
s = "321"
WritePrivateProfileStringByKeyName&"option", "url", s, inifile '修改
MsgBox "OK"
end if
End Sub
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)