首页 > 编程 > .NET > 正文

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

2024-07-10 13:07:12
字体:
来源:转载
供稿:网友


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


    '函数名: sdgetiniinfo
    '功能:读取ini文件设置信息
    '参数说明:inifile-->ini文件     inisection--ini文件中设置的部分名称
    '作者:sd
    '日期:2005-10-11
    'email:[email protected]
    '备注:转载或修改请保留此信息
    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

 

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表