- 按键认证大神
- 728264
- 3501
- 18
- 1898 朵
- 27453 个
- 1717 个
- 82980
- 2011-08-04
|
1#
t
T
发表于 2022-10-02 14:38
|
|只看楼主
函数名称:ReadIni 从配置文件中获取指定小节指定键的值 参数定义:Section 字符串型:小节名Key 字符串型:键名FilePath 字符串型:ini文件所在路径返回值:字符串型:读取到的内容调用例子:Text = ReadIni("s", "k", "C:\cfg.ini")函数名称:WriteIni 向配置文件中指定小节写入键及其值 参数定义:Section 字符串型:小节名Key 字符串型:键名Value 字符串型:键值FilePath 字符串型:ini文件所在路径返回值:整数型:非0为成功,否则为失败。调用例子:ret = WriteIni("s", "k", 1, "C:\cfg.ini")函数名称:OverwriteIni 向配置文件中指定小节覆盖写入键及其值(写入前会清空指定小节下所有键及其值) 参数定义:Section 字符串型:小节名Key 字符串型:键名和值,格式为"键=值",多个内容用"|"进行连接,比如"k1=1|k2=2|k3=3"FilePath 字符串型:ini文件所在路径返回值:整数型:非0为成功,否则为失败。调用例子:ret = OverwriteIni("s", "k1=1|k2=2|k3=3", "C:\cfg.ini")函数名称:EnumIniSection 从配置文件中获取所有小节 参数定义:FilePath 字符串型:ini文件所在路径返回值:字符串型:读取到的内容,多个内容用"|"连接,比如"s1|s2|s3"调用例子:Text = EnumIniSection("C:\cfg.ini")函数名称:EnumIniKey 从配置文件中获取指定小节所有键 参数定义:Section 字符串型:小节名FilePath 字符串型:ini文件所在路径返回值:字符串型:读取到的内容,多个内容用"|"连接,比如"k1|k2|k3"调用例子:Text = EnumIniKey("s", "C:\cfg.ini")函数名称:EnumIniKeyEx 从配置文件中获取指定小节所有键及其值 参数定义:Section 字符串型:小节名FilePath 字符串型:ini文件所在路径返回值:字符串型:读取到的内容,格式为"键=值",多个内容用"|"连接,比如"k1=1|k2=2|k3=3"调用例子:Text = EnumIniKeyEx("s", "C:\cfg.ini")函数名称:DeleteIni 从配置文件中删除指定小节的键及其值 参数定义:Section 字符串型:小节名Key 字符串型:键名,若键名为空则删除小节下所有键及其值FilePath 字符串型:ini文件所在路径返回值:整数型:非0为成功,否则为失败。调用例子:ret = DeleteIni("s", "k", "C:\cfg.ini")ret = DeleteIni("s", "", "C:\cfg.ini") 代码调试:- Dim Section, Key, Value, FilePath
- Section = "section"
- Key = "key"
- Value = "value"
- FilePath = "C:\test.ini"
- TracePrint "写入结果:" & WriteIni(Section, Key, Value, FilePath)
- TracePrint "覆盖写入结果:" & OverwriteIni(Section, "key=123|abc=456", FilePath)
- TracePrint "读取结果:" & ReadIni(Section, Key, FilePath)
- TracePrint "枚举小节结果:" & EnumIniSection(FilePath)
- TracePrint "枚举键结果:" & EnumIniKey(Section, FilePath)
- TracePrint "枚举键和值结果:" & EnumIniKeyEx(Section, FilePath)
- TracePrint "删除键结果:" & DeleteIni(Section, Key, FilePath)
- TracePrint "删除所有键结果:" & DeleteIni(Section, "", FilePath)
复制代码 调试结果: 脚本 利用API操作配置文件.Q ,第11行:写入结果:1 脚本 利用API操作配置文件.Q ,第12行:覆盖写入结果:1 脚本 利用API操作配置文件.Q ,第13行:读取结果:123 脚本 利用API操作配置文件.Q ,第14行:枚举小节结果:section 脚本 利用API操作配置文件.Q ,第15行:枚举键结果:key|abc 脚本 利用API操作配置文件.Q ,第16行:枚举键和值结果:key=123|abc=456 脚本 利用API操作配置文件.Q ,第17行:删除键结果:1 脚本 利用API操作配置文件.Q ,第18行:删除所有键结果:1
全部源码:
- Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
- Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpApplicationName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
- Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (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 String, ByVal lpFileName As String) As Long
- Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpApplicationName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
- Dim Section, Key, Value, FilePath
- Section = "section"
- Key = "key"
- Value = "value"
- FilePath = "C:\test.ini"
- TracePrint "写入结果:" & WriteIni(Section, Key, Value, FilePath)
- TracePrint "覆盖写入结果:" & OverwriteIni(Section, "key=123|abc=456", FilePath)
- TracePrint "读取结果:" & ReadIni(Section, Key, FilePath)
- TracePrint "枚举小节结果:" & EnumIniSection(FilePath)
- TracePrint "枚举键结果:" & EnumIniKey(Section, FilePath)
- TracePrint "枚举键和值结果:" & EnumIniKeyEx(Section, FilePath)
- TracePrint "删除键结果:" & DeleteIni(Section, Key, FilePath)
- TracePrint "删除所有键结果:" & DeleteIni(Section, "", FilePath)
- Function ReadIni(Section, Key, FilePath) '从配置文件中获取指定小节指定键的值
- str = space(32767)
- Dim ret
- ret = GetPrivateProfileString(Section, Key, "", str, Len(str), FilePath)
- If ret = 0 Then
- ReadIni = ""
- Else
- ReadIni = Left(str, ret)
- End If
- End Function
- Function WriteIni(Section, Key, Value, FilePath) '向配置文件中指定小节写入键及其值
- WriteIni = WritePrivateProfileString(Section, Key, Value, FilePath)
- End Function
- Function OverwriteIni(Section, Key, FilePath) '向配置文件中指定小节覆盖写入键及其值
- Key = Replace(Key, "|", Chr(0))
- Key = Key & Chr(0)
- OverwriteIni = WritePrivateProfileSection(Section, Key, FilePath)
- End Function
- Function EnumIniSection(FilePath) '从配置文件中获取所有小节
- str = space(32767)
- Dim ret
- ret = GetPrivateProfileSectionNames(str, Len(str), FilePath)
- If ret = 0 Then
- EnumIniSection = ""
- Else
- str = Replace(Left(str, InStr(str, Chr(0) & Chr(0))), Chr(0), "|")
- EnumIniSection = Left(str, Len(str) - 1)
- End If
- End Function
- Function EnumIniKey(Section, FilePath) '从配置文件中获取指定小节所有键
- str = space(32767)
- Dim ret
- ret = GetPrivateProfileString(Section, vbNullString, "", str, Len(str), FilePath)
- If ret = 0 Then
- EnumIniKey = ""
- Else
- str = Replace(Left(str, InStr(str, Chr(0) & Chr(0))), Chr(0), "|")
- EnumIniKey = Left(str, Len(str) - 1)
- End If
- End Function
- Function EnumIniKeyEx(Section, FilePath) '从配置文件中获取指定小节所有键及其值
- str = space(32767)
- Dim ret
- ret = GetPrivateProfileSection(Section, str, Len(str), FilePath)
- If ret = 0 Then
- EnumIniKeyEx = ""
- Else
- str = Replace(Left(str, InStr(str, Chr(0) & Chr(0))), Chr(0), "|")
- EnumIniKeyEx = Left(str, Len(str) - 1)
- End If
- End Function
- Function DeleteIni(Section, Key, FilePath) '从配置文件中删除指定小节的键及其值
- If Key = "" Then
- DeleteIni = WritePrivateProfileString(Section, vbNullString, vbNullString, FilePath)
- Else
- DeleteIni = WritePrivateProfileString(Section, Key, vbNullString, FilePath)
- End If
- End Function
复制代码
|