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
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 Longdim IniString As String * 255, I%
Dim x As Variant
dim tmpInt as intger
dim tmpStr as string
'读int类型ini,格式(段名,字段名,默认值,路径)
tmpIni = GetPrivateProfileInt("KeyUse", "skiptobnet ", 0, Path$)
'读string类型ini格式(段名,字段名,默认值,用于储存字段值的变量,字符长度,路径)
I = GetPrivateProfileString("KeyUse", "skiptobnet ", "*", IniString, 255, Path$)
'写ini格式(段名,字段名,需要写入的值,注意必须先转换为string,路径)
x = WritePrivateProfileString("KeyUse", "skiptobnet ", CStr(skiptobnet ), Path$)
vb说
Dim tmpInt As intger这一句用户定义类型未定义能不能写的傻瓜点,就是按我说的写下,就是写好能运行的代码?ini文件有这些:
[LAUNCHING]
skiptobnet = 1
[GENERAL]
ActivePlugin=1
读取“E:\plu.ini”
查找“skiptobnet “ ”ActivePlugin“
如果有
删除“skiptobnet “ ”ActivePlugin“
保存(只读也能保存)
如果没有
结束程序
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "INIFile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option ExplicitDim Ret As Long
Dim Start As Long
Private FileName As String
Const BufSize = 10240
Dim buf As String * BufSizePrivate 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
Public Sub SetFileName(ArgFileName As String)
FileName = ArgFileName
End Sub
Public Sub SetValue(ByVal clsName As String, ByVal key As String, ByVal V As String)
Ret = WritePrivateProfileString(clsName, key, V, FileName)
End Sub 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保存为inifile.cls
这是我自己写的ini读取写入类模块,可以直接引用到工程中去。
ini内格式:[类别名]
键=键值SetFileName(参数为ini文件的全路径文件名)
GetValue(类别名,键) 返回键值
SetValue(类别名,键,键值)
楼上的能说一下区别吗?下次我也用
ActivePlugin_Value = GetPrivateProfileInt("GENERAL", "ActivePlugin", -1, "E:\plu.ini")
这是ini内容,是这种格式吗?
[LAUNCHING]
Param=
Library=PlugY.dll
skiptobnet = 1 [GENERAL]
ActivePlugin=1
ActiveLogFile=0
DllToLoad=
DllToLoad2=
ActiveCommands=1
ActiveCheckMemory=0
[LANGUAGE]
;ENG|ESP|DEU|FRA|POR|ITA|JPN|KOR|SIN|CHI|POL|RUS
ActiveChangeLanguage=1
SelectedLanguage=CHI
[SAVEPATH]
ActiveSavePathChange=1
SavePath=MDSave\
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 LongPrivate Sub Command1_Click()
Dim skiptobnet_Value As Long, ActivePlugin_Value As Longskiptobnet_Value = GetPrivateProfileInt("LAUNCHING", "skiptobnet", -1, "E:\plu.ini")
Debug.Print "skiptobnet = " & skiptobnet_ValueActivePlugin_Value = GetPrivateProfileInt("GENERAL", "ActivePlugin", -1, "E:\plu.ini")
Debug.Print "ActivePlugin = " & ActivePlugin_Value
End Subskiptobnet = 1
ActivePlugin = 1