[vb]代码库
标准模块:
=============================
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 = Trim(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
=============================
调用方法:
读取:
iniFileName = "D:\XXX.ini" '设置ini文件的完整路径(也可以用其它扩展名)
Form1.Caption = GetIniS("设置", "窗口标题")
写入:
Dim INI As String
iniFileName = "D:\XXX.ini" '设置ini文件的完整路径(也可以用其它扩展名)
INI = SetIniS("设置", "窗口标题", Form2.Caption) '在"设置"-"窗口标题"写入了Form2的标题
INI = SetIniS("设置", "窗口标题", "") '只删除了"窗口标题"下的值。
删除:
Dim INI As String
iniFileName = "D:\XXX.ini" '设置ini文件的完整路径(也可以用其它扩展名)
INI = DelIniSec("设置") '删除整个字段。
INI=DelIniKey("设置", "窗口标题")
========================
附INI文件格式
[section]
name = value
如:
[设置]
窗口标题="Windows Internet Explorer"
中级程序员
by: 信惠导购返利 发表于:2013-11-24 21:03:24 顶(0) | 踩(0) 回复
很实用
回复评论