简单讲述VB.NET读取INI

VB.NET还是比较常用的,于是我研究了一下VB.NET读取INI,在这里拿出来和大家分享一下,希望对大家有用。

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

 
 
 
  1. Function sdGetIniInfo(ByVal iniFile As String, 
    ByVal iniSection As String) As String  
  2. If Not File.Exists(iniFile) Then  
  3. Return "文件 " & iniFile & " 未找到,请确认路径和文件名是否正确!"  
  4. Exit Function  
  5. End If  
  6.  
  7. Dim iniRead As New StreamReader(iniFile)  
  8. Dim iniStr As String = iniRead.ReadToEnd  
  9.  
  10. Dim i As Integer  
  11. Dim cLine As Integer  
  12. Dim noSec As Boolean = False 
  13. Dim getValue As String = "" 
  14. Dim cLst  
  15.  
  16. cLst = iniStr.Split(Chr(13))  
  17. cLine = UBound(cLst)  
  18.  
  19. For i = 0 To cLine  
  20. If cLst(i).indexof("=") > 0 Then  
  21. If cLst(i).split("=")(0).trim() = iniSection Then  
  22. noSec = True 
  23. getValue = cLst(i).split("=")(1).trim()  
  24. Exit For  
  25. End If  
  26. End If  
  27. Next  
  28.  
  29. If noSec = True Then  
  30. Return getValue  
  31. Else  
  32. Return "没有找到 " & iniSection & " 的设置信息!"  
  33. End If  
  34. End Function 

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

set.ini文件内容:

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

以上介绍VB.NET读取INI。

【编辑推荐】

  1. 简单分析VB.NET临时文件
  2. 详细描述VB.NET PadLeft方法
  3. VB.NET Shared变量经验总结
  4. 全面讲解VB.Net赋值语句
  5. 浅析VB.NET创建PPC客户端程序
THE END