温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB.NET如何读取INI

发布时间:2021-11-30 15:48:15 来源:亿速云 阅读:506 作者:小新 栏目:编程语言

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI