使用vb.net 2010在文本文件中搜索特定字符串

时间:2012-05-18 08:51:32

标签: vb.net ini

我想访问一个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
...

示例:我希望在bFXAAEnabled的值为1时检查我的表单中的复选框8,如果是0则取消选中。

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

5 个答案:

答案 0 :(得分:2)

你可以试试这个

    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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分:0)

在您的底部案例声明中,您将在任何一个evetuallity中将Checkbox8.checked属性设置为false。确定您需要将复选框设置为选中的情况并写入相应的行来执行此操作。

答案 4 :(得分:0)

从您的评论推断到我的其他答案我发布了一个关于如何从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
相关问题