vb 修改ini文件

vb 修改ini文件,第1张

你没有给出这几个ini键所在的项。一般INI的格式是这样的 [项名称]键名称=键值.....读写INI文件,可以使用API,以下为API引用声明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

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


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/56683.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-02-26
下一篇2023-02-26

发表评论

登录后才能评论

评论列表(0条)

    保存