千家信息网

VB.NET如何读取INI

发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,这篇文章主要介绍VB.NET如何读取INI,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!虽然VB.NET中读取XML配置信息很方便,但有时开发的过程中还是要用到INI文件,VB
千家信息网最后更新 2025年02月07日VB.NET如何读取INI

这篇文章主要介绍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. Dim iniRead As New StreamReader(iniFile)

  7. Dim iniStr As String = iniRead.ReadToEnd

  8. Dim i As Integer

  9. Dim cLine As Integer

  10. Dim noSec As Boolean = False

  11. Dim getValue As String = ""

  12. Dim cLst

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

  14. cLine = UBound(cLst)

  15. For i = 0 To cLine

  16. If cLst(i).indexof("=") > 0 Then

  17. If cLst(i).split("=")(0).trim() = iniSection Then

  18. noSec = True

  19. getValue = cLst(i).split("=")(1).trim()

  20. Exit For

  21. End If

  22. End If

  23. Next

  24. If noSec = True Then

  25. Return getValue

  26. Else

  27. Return "没有找到 " & iniSection & " 的设置信息!"

  28. End If

  29. 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"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

0