文件名称:ini配置文件操作代码
文件大小:8KB
文件格式:RAR
更新时间:2012-04-21 10:24:30
VB 源码
VB对ini配置文件进行读写操作实例 '''''''''''''''''''''''''''''''' '**************************************************************************** '描 述:根据鼠标位置获取句柄类名 '编 程:sysdzw 收藏整理 '发布日期:2008/07/20 '博 客:http://hi.baidu.com/sysdzw 'Email :sysdzw@163.com 'QQ :171977759 '**************************************************************************** Option Explicit 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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long Public iniFileName As String '配置文件的名称,一般在窗体load事件中初始化 '获取Ini的值,注意DefString表示如果不存在对应的KeyWord就设置此项为DefString,为空时不处理 Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, Optional ByVal DefString As String) As String Dim ResultString As String * 144, Temp% Dim s$, i% Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, iniFileName) '检索关键词的值 If Temp% > 0 Then '关键词的值不为空 For i = 1 To 144 If Asc(Mid$(ResultString, i, 1)) <> 0 Then s = s & Mid$(ResultString, i, 1) End If Next Else Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, iniFileName) '将缺省值写入INI文件 s = DefString End If GetIniS = s End Function '写入字符串值,返回值如果是0表示操作失败 Public Function SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String) As Boolean SetIniS = WritePrivateProfileString(SectionName, KeyWord, ValStr, iniFileName) End Function '清除 Section"段" Public Function DelIniSec(ByVal SectionName As String) As Boolean DelIniSec = WritePrivateProfileString(SectionName, 0&, "", iniFileName) End Function ''清除KeyWord"键" Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String) As Boolean DelIniKey = WritePrivateProfileString(SectionName, KeyWord, 0&, iniFileName) End Function '注意点: 'ini文件在有回车换行符会出错,经过测试,汉字要小于86字节, '英文要小于143字节才能返回列表框。 '使用范例: 'form_load时设置文件路径 'Private Sub Form_Load() ' iniFileName = App.Path & "\test.ini" 'End Sub ''写入 'Private Sub Command1_Click() ' MsgBox SetIniS(Text1.Text, Text2.Text, Now) 'End Sub ''获取 'Private Sub Command2_Click() ' MsgBox GetIniS(Text1.Text, Text2.Text, "fff") 'End Sub ''删除 Section 'Private Sub Command3_Click() ' MsgBox DelIniSec(Text1.Text) 'End Sub ''删除 KeyWord 'Private Sub Command4_Click() ' MsgBox DelIniKey(Text1.Text, Text2.Text) 'End Sub '''''''''''''''''''''''''''''''''''''''''''
【文件预览】:
ini配置文件操作(模块化)
----Form1.frm(3KB)
----工程1.vbp(688B)
----MSSCCPRJ.SCC(190B)
----Module1.bas(3KB)
----工程1.vbw(80B)
----工程1.exe(24KB)
----test.ini(89B)