VB.NET中读取INI文件设置信息函数sdGetIniInfo

本文介绍了一个VB.NET中用于读取INI文件设置信息的函数。该函数通过解析INI文件内容来获取指定部分的配置信息,并提供了使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虽然VB.NET中读取XML配置信息很方便,但有时开发的过程中还是要用到INI文件,在VB.NET中读取INI却不像VB中那么方便了,刚才写了个函数,现贴出来,也许各位能用得上。


'函数名: sdGetIniInfo
'功能:读取INI文件设置信息
'参数说明:iniFile-->INI文件 iniSection--INI文件中设置的部分名称
'作者:SD
'日期:2005-10-11
'Email:ztqas@126.com
'备注:转载或修改请保留此信息
Function sdGetIniInfo(ByVal iniFile As String, ByVal iniSection As String) As String
If Not File.Exists(iniFile) Then
Return "文件 " & iniFile & " 未找到,请确认路径和文件名是否正确!"
Exit Function
End If

Dim iniRead As New StreamReader(iniFile)
Dim iniStr As String = iniRead.ReadToEnd

Dim i As Integer
Dim cLine As Integer
Dim noSec As Boolean = False
Dim getValue As String = ""
Dim cLst

cLst = iniStr.Split(Chr(13))
cLine = UBound(cLst)

For i = 0 To cLine
If cLst(i).indexof("=") > 0 Then
If cLst(i).split("=")(0).trim() = iniSection Then
noSec = True
getValue = cLst(i).split("=")(1).trim()
Exit For
End If
End If
Next

If noSec = True Then
Return getValue
Else
Return "没有找到 " & iniSection & " 的设置信息!"
End If
End Function

说明:在引用的面页中要先引用 Imports System.IO

EG:

set.ini文件内容:

[Info]
name=zhuang
age=20

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name As String
name = sdGetIniInfo(Application.StartupPath & "\set.ini", "name")
MsgBox(name)
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值