首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> VB.NET >> 正文

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

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

    ''''函数名: sdGetIniInfo
    ''''功能:读取INI文件设置信息
    ''''参数说明:iniFile-->INI文件     iniSection--INI文件中设置的部分名称
    ''''作者:SD
    ''''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 

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航