[快捷功能]: | | |

读写INI的模块

[不指定 2007年1月23日星期二 02:32 | by 九天狼 | 出处:本站原创 ] | |
↓看看这下面↓
↑诱惑你自己↑
一个VB读写INI文件和模块代码


 '同样是添加一个模块,把这些代码复制到模块中即可,使用方法在注释中.
   '*******************************************
   'INI文件读写的API  By 九天狼
   'lpApplicationName(lpAppName) 节名;
   'lpKeyName 键名;lpString 键值;
   'lpFileName INI文件;
   'nDefault 没有键值时的默认值;
   'lpReturnedString 将取到的键值赋予的函数;
   'nSize 取键值的长度
   '*******************************************

   '*******************************************
   '取一个指定的Int键值
Public 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
   '取一个指定长度的Int键值并把它赋予一个函数
Public 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 Integer, ByVal lpFileName As String) As Integer
   '写一个指定的Int键值
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
   '取一个指定长度的Int节名
Public 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
   '写一个指定的Int节值
Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Public IniFile As String

'*******************************************
'SectionName 小节
'KeyName 键名
'KeyValue 键值
'IniFile Ini文件路径
'*******************************************

'*******************************************
' 将一个小节,键,键值写入*.ini文件中
'*******************************************
Public Function WriteIni(SectionName As String, KeyName As String, KeyValue As String)
   WritePrivateProfileString SectionName, KeyName, KeyValue, IniFile
End Function

Public Function ReadIni(SectionName As String, KeyName As String) As String
   Dim ret As Long
   Dim buff As String
   buff = String(500, 0)
   ret = GetPrivateProfileString(SectionName, KeyName, "", buff, 500, IniFile)
   '过滤字符串中间的空格,如果中间有空格,只取左边部分
   '    Dim i As Integer
   '    i = InStr(buff, Chr(0))
   '    If i <> 0 Then
   '        buff = Left(buff, i - 1)
   '    End If
   ReadIni = Trim(buff)
End Function

Public Function GetIni(SectionName As String, KeyName As String) As String
   GetIni = GetPrivateProfileInt(SectionName, KeyName, "", IniFile)
End Function

'*******************************************
' 从*.ini文件中删除一个键(包括删除该键和键所对应的键值)
'*******************************************
Public Function IniDelKey(SectionName As String, KeyName As String) As String
   WritePrivateProfileString SectionName, KeyName, 0&, IniFile
End Function

'*******************************************
' 从*.ini文件中删除一个小节(包括删除该小节和该小节下所有键和键值)
'*******************************************
Public Function IniDelSectionAll(SectionName As String) As String
   WritePrivateProfileString SectionName, vbNullString, vbNullString, IniFile
End Function

'*******************************************
' 从*.ini文件中删除一个小节下所有键和键值(保留该小节)
'*******************************************
Public Function IniDelAllKey(SectionName As String) As String
   WritePrivateProfileSection SectionName, "", IniFile
End Function

'*******************************************
'读取*.ini文件中的所有小节
'===========================================
'lIniFilename   -   *.ini文件名
'SectionArry()  -   存储返回的项目
'===========================================
'函数返回数组的最大下标值?(数组最小下标值为0)
'若没有找到任何值则函数返回 -1
'*******************************************
Public Function GetAllSection(SectionArry() As String) As Long
   Dim s As String
   Dim i, Max As Integer
   
   s = Space(1024)
   GetPrivateProfileString vbNullString, vbNullString, vbNullString, s, 1024, IniFile
   
   SectionArry = Split(s, Chr(0))
   MsgBox SectionArry(0)
   Max = UBound(SectionArry) - 2
   If Max >= 0 Then
       ReDim Preserve SectionArry(Max)
   End If
   GetAllSection = Max
End Function

下载文件源代码:
下载文件
这个文件只能在登入之后下载。请先 注册登入


最后编辑: 九天狼 编辑于2007年9月13日星期五 21:53
Tags: , ,
VB代码 | 评论(0) | 引用(0) | 阅读(3232)
您也可用OpenID登入:
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]