VB.NET读取INI文件设置信息函数sdGetIniInfo
敬业的IT人
互联网
佚名
2008-5-23 12:06:59
虽然VB.NET中读取XML配置信息很方便,但有时开发的过程中还是要用到INI文件,在VB.NET中读取INI却不像VB中那么方便了,刚才写了个函数,现贴出来,也许各位能用得上。
''''函数名: sdGetIniInfo
''''功能:读取INI文件设置信息
''''参数说明:iniFile-->INI文件 iniSection--INI文件中设置的部分名称
程序代码 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
''''函数名: sdGetIniInfo
''''功能:读取INI文件设置信息
''''参数说明:iniFile-->INI文件 iniSection--INI文件中设置的部分名称
程序代码 Function sdGetIniInfo(ByVal iniFile As String, ByVal iniSection As String) As StringIf 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
- 最新文章
- 用VB.net制作一个小程序(1)[05-23]
- 一个用Wsh来控制SqlServer的Dcom的VBs[05-23]
- 细说VB.NET(1)[05-23]
- Visual Basic.NET带来的新方法(1)[05-23]
- vb.net类的封装,继承,多态,抽象之一[05-23]
- 如何利用VB类提高代码质量[05-23]
- 相关文章
- 用VB.net制作一个小程序(1)[05-23]
- 细说VB.NET(1)[05-23]
- vb.net类的封装,继承,多态,抽象之一[05-23]
- VB.NET中LISTVIEW排序[05-23]
- 定制VB.NET控件编程之拦截击键动作(1)[05-23]
- 与VB.NET一起使用.NET Framework(1)[05-23]
