阅读VB .NET 2012中的INI文件部分

时间:2014-03-31 12:04:32

标签: vb.net ini

你能帮我解决这个问题吗?我想获取INI文件的节名称和字段。例如:

[connection]
server=localhost
user=root
password=root

我的程序应该返回部分名称和字段: 连接 服务器 用户 密码

提前致谢..

1 个答案:

答案 0 :(得分:0)

以防有人需要它。这是:

   Dim path As String = Application.StartupPath & "\path_to_file"

' get a list of the files in this directory
' -----------------------------------------

    Dim file_list As String() = Directory.GetFiles(path, "*.ini")

' go through the list of files 
' -------------------------------------------------------------------
  For Each f As String In file_list

    Dim a = New System.IO.FileInfo(f).Name

    ' open the file and read it
    ' -------------------------
    Dim sr As StreamReader = New StreamReader(f)

    ' read through the file line by line
    ' -----------------------------------
    Do While sr.Peek() >= 0
        Dim temp_name = Split(sr.ReadLine(), "=")
        first_part = temp_name(0)
        second_part = temp_name(1)

        ' do something with the information here

    Loop

    sr.Close()

  Next
相关问题