为什么这个streamreader objreader没有返回任何值?

时间:2014-05-21 11:40:42

标签: vb.net io streamreader

    Dim diar1 As IO.FileInfo() = ContractDirectory.GetFiles()
    Dim dra As IO.FileInfo
    Dim CC As Integer
    Dim c As Integer

    For Each dra In diar1
        CC = CC + 1
    Next
    Dim Contracts(CC) As Entry
    CC = 0

    For Each dra In diar1

        Dim FILE_NAME As String = "C:\Users\Documents\" & dra.Name
        Dim TextLine As String
        Dim Textline2 As String


        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            Do While objReader.Peek() <> -1
                TextLine = objReader.ReadLine() & vbNewLine
                Textline2 = objReader.ReadLine()
                If (Textline2 = "@#1") Then
                    Contracts(CC).Cnumber = TextLine
                End If

                If (Textline2 = "@#2") Then
                    Contracts(CC).Ctitle = TextLine
                End If

                If (Textline2 = "@#3") Then
                    Contracts(CC).Contype = TextLine
                End If

                If (Textline2 = "@#4") Then
                    Contracts(CC).Cdeadline = TextLine
                End If

                If (Textline2 = "@#5") Then
                    Contracts(CC).Devcategory = TextLine
                End If

                If (Textline2 = "@#6") Then
                    Contracts(CC).Sdesc = TextLine
                End If

                If (Textline2 = "@#7") Then
                    Contracts(CC).Fdesc = TextLine
                End If

                If (Textline2 = "@#8") Then
                    Contracts(CC).Imgid = TextLine
                End If
                CC = CC + 1


            Loop

        Else
            MsgBox("File Does Not Exist")
        End If

    Next
    TextBox1.Text = Contracts(1).Cnumber
    TextBox2.Text = Contracts(1).Ctitle
    ComboBox1.Text = Contracts(1).Contype
    DateTimePicker1.Text = Contracts(1).Cdeadline
    ComboBox2.Text = Contracts(1).category
    TextBox4.Text = Contracts(1).Sdesc
    TextBox5.Text = Contracts(1).Fdesc

我正在尝试将许多文本文件中的数据读入数组。文本文件如下所示。

Data
@#1
Data
@#2
Data
@#3
Data
@#4
Data
@#5
Data
@#6

文本文件已满,@#用于排序到正确的数组条目。你能看出它有什么问题吗?

UPDATE 所以也许我不理解streamreader,然后.readline auto会转到下一行。因为我认为这就是vbnewline所做的。但这是变量格式化的。

我需要它读取a行然后下一行并在循环中读取下一行2,依此类推。 所以,如果我知道nextline命令或其他什么,我可以在循环中没有循环。

1 个答案:

答案 0 :(得分:0)

如果有人偶然发现这个愚蠢的问题。我补充说:

 For Each dra In diar1
        CC = CC + 1
    Next
    Dim Contracts() As Contract
    ReDim Contracts(CC)
    For c = 0 To CC
        Contracts(c) = New Contract
    Next

干杯球员