vb 读写配置文件

vb 读写配置文件,第1张

下面这些代码可以实现对.INI文件的读写,用标准模块保存它..

Attribute VB_Name = "INIReadWrite"

Private 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

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

''lpApplicationName 要在其中写入新字串小节名称。这个字串不区分大小写

''lpszSection String 指定要在其中写入新串的小节。如尚不存在,会创建这个小节。这个字串不区分大小写。

''lpszKeyName String 要设置的项名或条目名。这个字串不区分大小写。用vbNullString可删除这个小节的所有设置项。

''lpszString String 指定为这个项写入的字串值。用vbNullString表示删除这个项现有的字串。

''lpFileName 读取文件名

Dim INIFilePath As String

Sub SetINI(ByVal INIPath As String)

INIFilePath = INIPath

End Sub

Sub WriteKey(ByVal Section As String, ByVal Key As String, Optional ByVal KeyValue As String = "")

WritePrivateProfileString Section, Key, KeyValue, INIFilePath

End Sub

Function ReadKey(ByVal Section As String, ByVal Key As String, Optional ByVal Size As Long = 256) As String

Dim Str As String

Str = Space(Size)

If GetPrivateProfileString(Section, Key, 0, Str, Size, INIFilePath) <>0 Then ReadKey = Str

End Function

窗体的话你可以自己设计下,因为不知道你想要什么效果,或者你可以就上面的代码用MsgBox输出Key值来看看效果..

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

用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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Private Function WriteIniFileString(StandKey As String, keyName As String, keyValue As String, FileName) As Long

'写INI文件函数

Dim leninikey As Long

Dim strkey As String * 255

WriteIniFileString = WritePrivateProfileString(StandKey, keyName, keyValue, FileName)

End Function

Private Function GetIniFileString(StandKey As String, keyName As String, Default As String, FileName As String) As String

'读取INI文件函数

Dim leninikey As Long

Dim strkey As String * 255

leninikey = GetPrivateProfileString(StandKey, keyName, Default, strkey, Len(strkey), FileName)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11867411.html

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

发表评论

登录后才能评论

评论列表(0条)

保存