现在基本上会写这样的ini 文件了
可是却有点不太懂,希望各位能帮忙给解说一下,谢谢!主要就是 定义的读写ini是自己写的那个函数,尤其是里面的参数,我看了别人写的,参数都是不一样的,可是却怎么也搞不懂。
希望各位能给个详细的解释,最好是结合段例子,呵呵。
不胜感激!!
可是却有点不太懂,希望各位能帮忙给解说一下,谢谢!主要就是 定义的读写ini是自己写的那个函数,尤其是里面的参数,我看了别人写的,参数都是不一样的,可是却怎么也搞不懂。
希望各位能给个详细的解释,最好是结合段例子,呵呵。
不胜感激!!
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
Private Sub Form_Load()
Dim Ret As String, NC As Long
'Write the setting to the file (c:\test.ini) under
' Project1 -> Keyname
WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"
'Create a buffer
Ret = String(255, 0)
'Retrieve the string
NC = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")
'NC is the number of characters copied to the buffer
If NC <> 0 Then Ret = Left$(Ret, NC)
'Show our string
MsgBox Ret
'Delete the file
Kill "c:\test.ini"
End Sub
给你一个完整代码,生成.dll文件后通用:)Option ExplicitPrivate 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 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 LongPrivate Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As LongPublic ErrorMsg As StringPrivate Sub Class_Initialize()
ErrorMsg = vbNullString
End Sub'写入----------------------------------
Public Function WriteString(iniFileName As String, Section As String, key As String, Value As String) As Boolean
WriteString = False
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
If WritePrivateProfileString(Section, key, Value, iniFileName) = 0 Then
ErrorMsg = "Failed to write to the ini file!"
Exit Function
End If
WriteString = True
End Function'读出字符串----------------------------
Public Function ReadString(iniFileName As String, Section As String, key As String, Size As Long) As String
Dim ReturnStr As String
Dim ReturnLng As Long
ErrorMsg = vbNullString
ReadString = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
ReturnStr = Space(Size)
ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, iniFileName)
ReadString = Left(ReturnStr, ReturnLng)
End Function'读出数值-----------------------------
Public Function ReadInt(iniFileName As String, Section As String, key As String) As Long
Dim ReturnLng As Long
ReadInt = 0
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
ReturnLng = GetPrivateProfileInt(Section, key, 0, iniFileName)
If ReturnLng = 0 Then
ReturnLng = GetPrivateProfileInt(Section, key, 1, iniFileName)
If ReturnLng = 1 Then
ErrorMsg = "Can not read the ini file!"
Exit Function
End If
End If
ReadInt = ReturnLng
End Function
WriteString = False
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
If WritePrivateProfileString(Section, key, Value, iniFileName) = 0 Then
ErrorMsg = "Failed to write to the ini file!"
Exit Function
End If
WriteString = True
End Function谁能帮忙给解释下这段啊,比如里面的参数,写INI文件都能这样写么
还有为什么我看到的怎么都是,出现了iniFileName = ""\
If WritePrivateProfileString(Section, key, Value, iniFileName) = 0这些情况啊,就是这些不成立的话,就写成功了么!???
烦请各位给一下稍微详细的解释
给加些注释也行,谢谢!
'**模 块 名:ini 读写模块
'**说 明:VB 的 INI 读写模块
'**创 建 人:冯铭
'**日 期:2006-06-30 16:38:38
'**修 改 人:
'**日 期:
'**描 述:
'**版 本:V1.0.0
'*************************************************************************Option Explicit
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpRetunedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function SaveINI Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFileName As String) As LongFunction GetINI(AppName As String, KeyName As String, filename As String) As String
Dim RetStr As String
RetStr = String(10000, Chr(0))
GetINI = Left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", RetStr, Len(RetStr), filename))
End Function'调用方法:
'读: Rtn = GetINI(AppName, KeyName, filename)
'写:SaveINI appname,keyname,string,filename'解释:例如INI文件中有这么一块:'[main] .........这就是appname,不包括[]号
'FontColor=801085 ............FontColor就是KeyName,801085就是String'而你的INI的绝对路径就是Filename'写日至文件