在写程序的时候,要用的INI文件来储存一些东西。而本菜鸟对那些高深的API几乎是一窍不通的。
那个ini文件在 D盘IP.ini ,里面有这样一段
[number]
number=3
我用高手写的函数把number=3的数值读取出来,要实现一下按钮,变成
number=4
再按
number=5
…………
另附,我用的读取ini文件的函数:
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
'以下函数,读ini文件,不固定节点,in_key为写入/读取的主键
'针对字符串值
'空值表示出错
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, "D:\IP.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function
请高手帮帮忙,小菜感激不禁
想实现你的目标,分三步走.
1.首先你得加上下面这一句:
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
2.再把你上面的所有代码复制到上面这句的下面.
3.然后在按钮里,代码如下:
Private Sub Command1_Click()
Dim NewVal As String
NewVal = CStr(Val(GetIniStr("number", "number")) + 1)
WritePrivateProfileString "number", "number", NewVal, "D:\IP.ini"
End Sub
这样就可以了
'Ini操作声明
'==================================================
Dim Ret As Long
Dim Start As Long
Public FileName As String
Public MaxthonPath As String
Public TimeID As Integer
Const BufSize = 10240
Dim buf As String * BufSize
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 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 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
'============================
'操作INI文件函数
'============================
Public Function SetValue(ByVal clsName As String, ByVal key As String, ByVal V As String) As Boolean
Ret = WritePrivateProfileString(clsName, key, V, FileName)
End Function
Public Function GetValue(ByVal clsName As String, ByVal key As String) As String
Ret = GetPrivateProfileString(clsName, key, "", buf, BufSize, FileName)
Start = 1
GetValue = RetStr()
End Function
Private Function RetStr() As String
Dim i As Long
i = InStr(Start, buf, Chr$(0))
If i > Start Then
RetStr = Mid$(buf, Start, i - Start)
End If
Start = i + 1
End Function
Sub Main()
FileName = IIf(Right$(App.path, 1) <> "\" And Right$(App.path, 1) <> "/", App.path & "\", App.path) & "config.ini" ' 这里的 config.ini改成你的文件名
Form1.Show '这里的Form1改成你要显示的窗体
End Sub
工程 > 工程1 属性 在通用标签 页中设置 启动对象 Sub Main
然后工程中就可以用
变量 = GetValue("number", "number")
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 WritePrivateProfileString _
Lib "kernel32" _
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Private Sub Command1_Click()
Dim i As Integer
i = GetPrivateProfileInt("number", "number", 0, "d:\ip.ini")
MsgBox i
i = i + 1
WritePrivateProfileString "number", "number", CStr(i), "d:\ip.ini"
i = GetPrivateProfileInt("number", "number", 0, "d:\ip.ini")
MsgBox i
End Sub