当前位置:编程学习 > VB >>

VB怎样读取这样的文件?

答案:ini文件是用来保存程序配置的。中括号里边的叫做“字段”,等号左边叫做关键字,等号右边是属性值。
字段主要用于把不同分类的配置隔开,或者当多个程序共用一个ini文件时,每个程序的配置单独放置。
要读取和写入ini文件的配置,请按以下步骤操作:

第一步:新建一个模块,复制进去下面的代码
Public iniFile As String
'存放ini配置文件的位置
'读写ini配置文件的函数
Public 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
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 Integer, _



ByVal lpFileName As String) _



As Integer
Public Declare Function WritePrivateProfileString Lib "kernel32" _



Alias "WritePrivateProfileStringA" _



(ByVal lpApplicationName As String, _



ByVal lpKeyName As String, _



ByVal lpString As String, _



ByVal lpFileName As String) _



As Long
'读取配置文件中的字符串
Public Function GetConfigString(ApplicationName As String, Key As String, Default As String) As String
Dim str As String * 256
Call GetPrivateProfileString(ApplicationName, Key, Default, str, 256, iniFile)
GetConfigString = Trim0(str)
End Function
'读取字符串中的整数
Public Function GetConfigInt(ApplicationName As String, Key As String, Default As Long) As Long
Dim i As Long
i = GetPrivateProfileInt(ApplicationName, Key, Default, iniFile)
GetConfigInt = i
End Function
'读取字符串中的布尔值
Public Function GetConfigBoolean(ApplicationName As String, Key As String, Default As Long) As Boolean
GetConfigBoolean = CBool(GetConfigString(ApplicationName, Key, str(Default)))
End Function
'写入配置文件
Public Sub PutConfigString(ApplicationName As String, Key As String, Value As String)
Call WritePrivateProfileString(ApplicationName, Key, Value, iniFile)
End Sub

第二步,设置ini文件路径
在主程序的Form_Load过程中:
iniFile = App.Path & "\\config.ini"
其中config.ini这个名字你可以随便改

第三步,写入配置
调用PutConfigString()即可。示例:
PutConfigString "YourProgramName","Width","1400"
进行完这一步之后,ini文件中就会出现下面的内容
[YourProgramName]
Width = 1400

第四步,读取配置
读取自己写入的就不演示了,给你说一个读取系统oem配置信息的
iniFile = "C:\\WINDOWS\\system32\\oeminfo.ini"
dim oem as string,ver as string
oem = getconfigstring("General","Manufacturer","")
ver = getconfigstring("General","Model","")
msgbox "这台电脑的制造商是:" & oem & vbnewline & "操作系统版本:" & ver
破解
你好,解压啊,希望采纳。,希望采纳。 
什么样的?

上一个:VB后面的数据库编程怎么学啊
下一个:用vb制作一个MP3播放器

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,