VB读取INI文件

                                        

Module1.bas代码:


Option Explicit

Private strINI As String
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 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 Sub CreateIni(strDrv As String, strDir As String)
strINI = MakePath(strDrv, strDir)
End Sub

Public Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub

Public Function GetIniKey(strSection As String, strKey As String) As String
Const myinifile = "/Info.ini"
strINI = myinifile
Dim strTmp As String
Dim lngRet As String
Dim i As Integer
Dim strTmp2 As String

strTmp = String$(1024, Chr(32))
lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)
strTmp = Trim(strTmp)
strTmp2 = ""
For i = 1 To Len(strTmp)
If Asc(Mid(strTmp, i, 1)) <> 0 Then
strTmp2 = strTmp2 + Mid(strTmp, i, 1)
End If
Next i
strTmp = strTmp2

GetIniKey = strTmp
End Function

Public Property Let iniFileName(ByVal New_IniPath As String)
strINI = New_IniPath
End Property

Public Property Get iniFileName() As String
iniFileName = strINI
End Property

Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
End Function

Public Function DelIniSec(ByVal SectionName As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
End Function
 Public Function AppProFileName(iniFileName)
 iniFileName = "/Info"
AppProFileName = App.Path & "/" & iniFileName & ".ini"
End Function

 

 Public Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String
Dim ResultString As String * 144, Temp As Integer
Dim s As String, i As Integer
Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, AppProFileName(iniFileName))
'检索关键词的值
If Temp% > 0 Then '关键词的值不为空
s = ""
For i = 1 To 144
If Asc(Mid$(ResultString, i, 1)) = 0 Then
Exit For
Else
s = s & Mid$(ResultString, i, 1)
End If
Next
Else
Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, AppProFileName(iniFileName))
'将缺省值写入INI文件
s = DefString
End If
GetIniS = s
End Function

窗体代码:

Private Sub Command1_Click()
Read_Info
End Sub
Private Sub Read_Info()

myIni.iniFileName = App.Path & myinifile
myIni.WriteIniKey "myInfo", "Name", Text1.Text
End Sub

Private Sub Command2_Click()
Text2.Text = myIni.GetIniS("myInfo", "Name", myinifile)
End Sub

Ini文件:

[myInfo]
Name=VB读取ini文件
Age=25
Phone=1363868746*
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在64位系统上,使用VB读取INI文件的方法与32位系统上基本相同。VB提供了一个`GetPrivateProfileString`函数,可以实现读取INI文件中的配置信息。 首先,在VB代码中引入`kernel32.dll`库,并声明`GetPrivateProfileString`函数: ```vb 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 ``` 然后,可以按照以下步骤读取INI文件中的配置信息: 1. 定义变量来存储INI文件路径和配置项的名称: ```vb Dim iniFilePath As String = "C:\path\to\config.ini" Dim sectionName As String = "Section1" Dim keyName As String = "Key1" ``` 2. 声明变量来存储读取到的配置信息: ```vb Dim valueBuffer As String = Space(255) '用于存储配置值的缓冲区 Dim valueLength As Long '配置值的长度 ``` 3. 调用`GetPrivateProfileString`函数来读取配置项的值: ```vb valueLength = GetPrivateProfileString(sectionName, keyName, "", valueBuffer, 255, iniFilePath) ``` 4. 检查读取结果,如果返回值大于0,则表示成功读取了配置项的值: ```vb If valueLength > 0 Then Dim value As String = Left(valueBuffer, valueLength) '提取配置值 MsgBox("配置项的值为:" & value) Else MsgBox("无法读取配置项的值。") End If ``` 通过以上步骤,就可以在64位系统上使用VB读取INI文件的配置信息了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值