将ini文件的部分加载到Visual Basic中的ComboBox

时间:2015-10-25 09:30:18

标签: vb.net visual-studio ini

希望有人可以提供帮助。 我想将外部ini文件的部分加载到Visual Basic 2015中的ComboBox中,然后将所选部分的键和值加载到TextBoxes中。 我一直在用头发拔掉头发,甚至无法开始如何实现这一目标。

非常感谢您给予的任何帮助。

谢谢, 丹

1 个答案:

答案 0 :(得分:1)

首先创建此类(Source - CodeProject Article):

Imports System.Text
Imports System.Runtime.InteropServices
Public Class INI

    <DllImport("kernel32")> _
    Private Shared Function GetPrivateProfileString(Section As String, Key As String, Value As String, Result As StringBuilder, Size As Integer, FileName As String) As Integer
    End Function


    <DllImport("kernel32")> _
    Private Shared Function GetPrivateProfileString(Section As String, Key As Integer, Value As String, <MarshalAs(UnmanagedType.LPArray)> Result As Byte(), Size As Integer, FileName As String) As Integer
    End Function

    <DllImport("kernel32")> _
    Private Shared Function GetPrivateProfileString(Section As Integer, Key As String, Value As String, <MarshalAs(UnmanagedType.LPArray)> Result As Byte(), Size As Integer, FileName As String) As Integer
    End Function

    Public path As String
    Public Sub New(INIPath As String)
        path = INIPath
    End Sub

    Public Function GetSectionNames() As String()
        Dim maxsize As Integer = 500
        While True
            Dim bytes As Byte() = New Byte(maxsize - 1) {}
            Dim size As Integer = GetPrivateProfileString(0, "", "", bytes, maxsize, path)
            If size < maxsize - 2 Then
                Dim Selected As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
                Return Selected.Split(New Char() {ControlChars.NullChar})
            End If
            maxsize *= 2
        End While
    End Function
    Public Function GetEntryNames(section As String) As String()
        Dim maxsize As Integer = 500
        While True
            Dim bytes As Byte() = New Byte(maxsize - 1) {}
            Dim size As Integer = GetPrivateProfileString(section, 0, "", bytes, maxsize, path)
            If size < maxsize - 2 Then
                Dim entries As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
                Return entries.Split(New Char() {ControlChars.NullChar})
            End If
            maxsize *= 2
        End While
    End Function
    Public Function GetEntryValue(section As String, entry As String) As Object
        Dim maxsize As Integer = 250
        While True
            Dim result As New StringBuilder(maxsize)
            Dim size As Integer = GetPrivateProfileString(section, entry, "", result, maxsize, path)
            If size < maxsize - 1 Then
                Return result.ToString()
            End If
            maxsize *= 2
        End While
    End Function
End Class

将部分添加到ComboBox,如下所示:

Dim _ini As New INI("somefile.ini")
ComboBox1.Items.AddRange(ini.GetSectionNames()) 'For all sections
ComboBox1.Items.Add("section1") 'For specific section(s)

然后,在ComboBox的选择更改事件中:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim _ini As New INI("somefile.ini")
    Dim section As String = ComboBox1.SelectedItem
    TextBox1.Text = _ini.GetEntryValue(section, "someKey") 'for specific entry
    For Each item In _ini.GetEntryNames(section) 'this is for all entries
        'do whatever you want here with the item variable like this:
        'TextBox1.Text = _ini.GetEntryValue(section, item)...
    Next
End Sub