VB读写INI文件

VB读写INI文件,第1张

给你两个读取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

使用api函数:给你个模块代码:

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 String, ByVal lpFileName As String) As Long

Public Function GetPPs(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String) As String

Dim Ret As String, NC As Long, ln As Long

ln = &H7FFF

Ret = String(ln, 0)

If Right(App.Path, 1) = "\" Then

NC = GetPrivateProfileString(lpApplicationName, lpKeyName, lpDefault, Ret, ln, App.Path &"SoftConfig.ini")

Else

NC = GetPrivateProfileString(lpApplicationName, lpKeyName, lpDefault, Ret, ln, App.Path &"\SoftConfig.ini")

End If

Ret = Left(Ret, NC)

Dim t As String

t = Ret

t = Replace(t, "{Chr(9)}", Chr(9))

t = Replace(t, "{Chr(10)}", Chr(10))

t = Replace(t, "{Chr(13)}", Chr(13))

t = Replace(t, "{Chr(32)}", Chr(32))

t = Replace(t, Chr(0), "")

GetPPs = t

End Function

Public Function WritePPs(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpKeyVal As String) As String

Dim t As String

t = lpKeyVal

t = Replace(t, Chr(9), "{Chr(9)}")

t = Replace(t, Chr(10), "{Chr(10)}")

t = Replace(t, Chr(13), "{Chr(13)}")

t = Replace(t, Chr(32), "{Chr(32)}")

If Right(App.Path, 1) = "\" Then

WritePrivateProfileString lpApplicationName, lpKeyName, t, App.Path &"SoftConfig.ini"

Else

WritePrivateProfileString lpApplicationName, lpKeyName, t, App.Path &"\SoftConfig.ini"

End If

End Function


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/55057.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-02-26
下一篇2023-02-26

发表评论

登录后才能评论

评论列表(0条)

    保存