1

有一个 INI 文件,我想从中访问和读取信息。这是相关 INI 文件的完整内容:http ://www.heypasteit.com/clip/0C3Q

有几个人向我推荐了代码,但它们不起作用。我相信这是因为[ ]那个 INI 文件中的标签。因为如果我删除标签,它们就会起作用。

我的程序有一堆组合框、轨迹栏和复选框。这些项目将由从 INI 文件中获取的信息填充。

例如,ini 文件有这些行;

...
bCrosshairEnabled=1
bDoDepthOfField=0
bFXAAEnabled=1
uiMaxSkinnedTreesToRender=10
iSize H=720
iSize W=1280
...

示例:我希望我的表单中的 checkbox8 被选中,如果bFXAAEnabled它的值是 ,1或者如果它是0

请确保您的代码兼容 VB.NET 2010。

4

5 回答 5

3

你可以试试这个

    Dim myvalue As Integer
    For Each k As String In IO.File.ReadLines("d:\test.txt")
        If k.Contains("valueccc=") Then
            Dim values() As String = k.Split(CChar("=")).ToArray
            myvalue = Convert.ToInt32(values(1))
        End If
    Next
Select case myvalue
case 1
case 2
End select
于 2012-05-18T08:59:56.260 回答
1

从您对我的其他答案的评论中推断,我发布了一个关于如何从 INI 文件中读取值的新答案:

Imports System.Text
Imports System.Runtime.InteropServices

Public Class TestForm
    'declare the API
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
                        ByVal lpKeyName As String, _
                        ByVal lpDefault As String, _
                        ByVal lpReturnedString As StringBuilder, _
                        ByVal nSize As Integer, _
                        ByVal lpFileName As String) As Integer
    End Function

    'Function to retrieve a value from an INI file
    Public Function GetINIValue(filename As String, section As String, key As String, Optional defaultValue As String = "") As String
        Dim res As Integer
        Dim sb As New StringBuilder(500)
        res = GetPrivateProfileString(section, key, "", sb, sb.Capacity, filename)
        If res = 1 Then Return sb.ToString Else Return defaultValue
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim filename As String = "C:\Scratch\Test.ini"
        CheckBox1.Checked = If(GetINIValue(filename, "Display", "bFXAAEnabled") = "1", True, False)
    End Sub

End Class
于 2012-05-18T11:08:33.390 回答
1
'Reads each line from the text file one at a time
    For Each line As String In IO.File.ReadLines("text file path")

        'split the string by equals sign
        Dim ary As String() = line.Split("="c)

        'Check the data type of the string we collected is inline with what we are expecting, e.g. numeric
        If IsNumeric(ary(1)) Then

            'create key value pair: the string before the equals and the number after it
            'e.g.
            'key = "valuexyz" | value = "36""
            Dim valuePair As New KeyValuePair(Of String, Integer)(ary(0), CInt(ary(1)))

            'obtain the string after the equals sign
            Dim value As Integer = CInt(ary(1))

            'based on the value after the equals sign, do something
            Select Case value
                Case 1
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
                Case 2
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
                Case Else
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
            End Select

        End If

    Next
于 2012-05-18T09:16:22.130 回答
0
    'remember to import this at the top of your class
    Imports System.IO


    Dim filename As String = "C:\file.txt"
    Dim parts() As String = Nothing

    'use a stream reader to read the file line by line
    Using sr As New StreamReader(filename)
        'read a line as split into parts at the equal sign
        parts = sr.ReadLine.Split("="c)
        'check we actually have read the data in the correct format
        If parts.Length >= 2 Then
            Select Case parts(0)
                'use a case statement for the left hand side
                Case "valuexyz"
                    'now use a case statement for the right hand side
                    Select Case parts(1).Trim
                        Case "0"
                            Foo()
                        Case "1"
                            Bar()
                    End Select

                Case "valueabc"
                    Select Case parts(1).Trim
                        Case "0"
                            Foo2()
                        Case "1"
                            Bar2()
                    End Select
            End Select
        End If
    End Using
于 2012-05-18T09:01:29.657 回答
0

在您的底例语句中,您将 Checkbox8.checked 属性设置为 false 在任何一种情况下。确定在哪种情况下需要将复选框设置为选中并编写相应的行来执行此操作。

于 2012-05-18T11:00:33.337 回答