VB读写INI文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB读写INI文件相关的知识,希望对你有一定的参考价值。

VB读写INI文件
如下:
[KeyName]
Key1=Value1
Key2=Value2
Key3=Value3
...
等等后面还有很多,我们读的时候不可能一个个列出来后再读指定字节里的值。应该是需要一个循环读取[KeyName]下所有的值。怎么来作呢?谢谢各高人了。

给你两个读取ini文件的过程吧

'读取配置文件
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
Long,
ByVal
lpFileName
As
String)
As
Long
'写入配置文件
Public
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
Function
ReadOption(ByVal
Caption
As
String,
ByVal
Item
As
String,
ByVal
Path
As
String)
As
String
'Caption小节名称;Item项名称;Path配置文件路径

On
Error
Resume
Next

Dim
sBuffer
As
String

sBuffer
=
Space(128)

GetPrivateProfileString
Caption,
Item,
vbNullString,
sBuffer,
128,
Path

ReadOption
=
Left(sBuffer,
InStr(sBuffer,
vbNullChar)
-
1)
End
Function
'写入配置文件
Public
Function
WriteOption(ByVal
Caption
As
String,
ByVal
Item
As
String,
ByVal
Key
As
String,
ByVal
Path
As
String)
'Caption小节名称;Item项名称;Key项值;Path配置文件路径

Dim
sBuffer
As
String

sBuffer
=
Space(128)

sBuffer
=
Key
&
vbNullChar

WriteOption
=
WritePrivateProfileString(Caption,
Item,
sBuffer,
Path)
End
Function
'获得配置文件路径
Public
Function
GetOptPath()
As
String

GetOptPath
=
App.Path
&
"\config.ini"
End
Function
参考技术A 'API函数的声明
'ini配置文件
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 GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal nDefault 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

'自定义函数
'读取/设置ini配置文件
Private Function GetIniS(FilePath As String, SectionName As String, KeyWord As String, DefString As String) As String
Dim ResultString As String * 255, Temp As Long
Dim s As String, i As Integer
Temp = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 255, FilePath)
If Temp > 0 Then '关键词的值不为空
s = ""
For i = 1 To 255
If Asc(Mid(ResultString, i, 1)) = 0 Then
Exit For
Else
s = s & Mid(ResultString, i, 1)
End If
Next i
Else
If DefString <> "" Then
'将缺省值写入INI文件
Temp = WritePrivateProfileString(SectionName, KeyWord, DefString, FilePath)
s = DefString
End If
End If
GetIniS = s
End Function

Private Function GetIniN(FilePath As String, SectionName As String, KeyWord As String, DefValue As Long) As Long
Dim d As Long, s As String
d = DefValue
GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, FilePath)
'If d <> DefValue Then
' s = CStr(d)
' d = WritePrivateProfileString(SectionName, KeyWord, s, FilePath)
'End If
End Function

Private Sub SetIniS(FilePath As String, SectionName As String, KeyWord As String, ValStr As String)
Dim res As Long
res = WritePrivateProfileString(SectionName, KeyWord, ValStr, FilePath)
End Sub

Private Sub SetIniN(FilePath As String, SectionName As String, KeyWord As String, ValLon As Long)
Dim res As Long, s As String
s = CStr(ValLon)
res = WritePrivateProfileString(SectionName, KeyWord, s, FilePath)
End Sub

'读取过程
Private Sub Command1_Click()
Dim fname As String
Dim SectionName As String, KeyWord As String, DefString As String, DefValue As Long
Dim s As String, sValue As String
Dim i As Integer, j As Integer
Dim Result() As String

fname = "......" '你所要读取的文件(包括其绝对路径)
SectionName = "KeyName" '字段
KeyWord = "Key" '相同的部分
DefString = "???" '一般为空字符,我这里是用于区分Value1为空是是否继续进行用的

i = 0
j = -1
Do
i = i + 1
s = KeyWord & CStr(i)
sValue = GetIniS(fname, SectionName, KeyWord, DefString)
If sValue <> DefString Then
j = j + 1
ReDim Preserve Result(j)
Result(j) = sValue
Else
Exit Do
End If
Loop

End Sub

你再结合自己的具体情况,修改下就行了本回答被提问者采纳

python3.9 读写ini文件

参考技术A 官方文档

从源码看出,这个ini文档,可以像字典一样,使用“:”,但一般我们都使用“=”,并且值都不需要加引号
备注可以使用'#'或';',单独一行,可缩进。
class ConfigParser(RawConfigParser):
class RawConfigParser(MutableMapping):

详细看官网,不搬砖,列出几个我自己常用的

以上是关于VB读写INI文件的主要内容,如果未能解决你的问题,请参考以下文章

vb.net读写txt文件的问题

vb.net 应用程序重新加载 INI 文件

C#中读写INI文件

python3.9 读写ini文件

python-inipython读写ini文件

python读写ini文件