Vb.net 通过单击按钮进行文件处理

Vb.net 通过单击按钮进行文件处理,vb.net,file-io,Vb.net,File Io,我写这段代码是为了将文本文件加载到表单中,但我一直在count变量上遇到溢出错误 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim fdlg As OpenFileDialog = New OpenFileDialog() Dim count, i As Integer count = 0

我写这段代码是为了将文本文件加载到表单中,但我一直在count变量上遇到溢出错误

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    Dim fdlg As OpenFileDialog = New OpenFileDialog()
    Dim count, i As Integer
    count = 0
    fdlg.Title = "Open text file to read"
    fdlg.InitialDirectory = "c:\"
    fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
    If fdlg.ShowDialog() = DialogResult.OK Then
        If System.IO.File.Exists(fdlg.FileName) = True Then
            Dim objReader As New System.IO.StreamReader(fdlg.FileName)
            Do While objReader.Peek() <> -1
                count = count + 1 'Overflow Error?
            Loop
            Dim myArray(count) As String
            For i = 0 To i = count
                myArray(i) = objReader.ReadLine()
            Next i
            For i = 0 To count
                TextBox1.Text = myArray(i) & vbNewLine
            Next i
            objReader.Close()
        Else
            MsgBox("File Does Not Exist")
        End If
    End If

End Sub
Private子按钮4\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮4。单击
Dim fdlg As OpenFileDialog=新建OpenFileDialog()
Dim count,i为整数
计数=0
fdlg.Title=“打开要读取的文本文件”
fdlg.InitialDirectory=“c:\”
fdlg.Filter=“所有文件(*.*)所有文件(*.*)所有文件(*.*)”
如果fdlg.ShowDialog()=DialogResult.OK,则
如果System.IO.File.Exists(fdlg.FileName)=True,则
Dim objReader作为新System.IO.StreamReader(fdlg.FileName)
Do While objReader.Peek()-1
计数=计数+1'溢出错误?
环
Dim myArray(计数)为字符串
对于i=0到i=count
myArray(i)=objReader.ReadLine()
接下来我
对于i=0进行计数
TextBox1.Text=myArray(i)&vbNewLine
接下来我
objReader.Close()
其他的
MsgBox(“文件不存在”)
如果结束
如果结束
端接头
同意@puropoix,Peek()读取下一个字符,但不移动光标。因此,很可能是无限循环,导致整数数据类型溢出。如果仅使用计数来调整数组大小,则可能有更有效的方法来读取文件:

Dim objReader As New System.IO.StreamReader("")
Dim myStrings As New List(Of String)
While Not objReader.EndOfStream()
   myStrings.Add(objReader.ReadLine())
End While
For Each aString In myStrings
    Textbox1.text &= aString & vbNewLine
Next
或者更好的方法是使用字符串生成器,因为它效率更高,尤其是当您试图添加到文本框时,逐行添加到文本框的效率非常低,与使用stringbuilder相比,它会使您的应用程序运行非常慢:

Dim objReader As New System.IO.StreamReader("")
Dim myStrings As New System.Text.StringBuilder
While Not objReader.EndOfStream()
    myStrings.AppendLine(objReader.ReadLine())
End While
Textbox1.text = myStrings.ToString()

这就是我提出的解决方案。我现在的问题是,有什么样的智能拆分函数“Delimiter”可以用来确保数组元素中只存储一个单词?假设存在随机文本文件格式:即每行一个单词,每行多个单词或带空格的行

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    Dim fdlg As OpenFileDialog = New OpenFileDialog()
    Dim count, i As Integer
    Dim textLine As String
    count = 0

    fdlg.Title = "Open text file to read"
    fdlg.InitialDirectory = "c:\"
    fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"

    If fdlg.ShowDialog() = DialogResult.OK Then
        If System.IO.File.Exists(fdlg.FileName) = True Then

            Dim objReader As New System.IO.StreamReader(fdlg.FileName)

            textLine = objReader.ReadToEnd()
            Dim myArray() As String = Split(textLine, vbCrLf)

            'For i = 0 To myArray.Length - 1
            TextBox1.Text = myArray(6)  'A test to see if myArray is populated
            ' Next i

            objReader.Close()

        Else

            MsgBox("File Does Not Exist")

        End If

    End If

End Sub

查看
Peek()
做了什么谢谢你指出Peek()的函数我现在更熟悉了。你的答复促使我探讨了不可变和可变变量的概念,我能看出你建议背后的道理。不幸的是,我正在尝试为基本拼写检查加载字典,并且需要使用二进制搜索对数组进行排序。这是我修改过的代码,但是我仍然不能用ReadLine()函数填充数组?我在这里遗漏了什么?我想这是难点,一个分隔符应该是1,并且只在1个分隔符上。让您的生活变得轻松,并使您的数据正常化。您可以使用分隔符,并通过使用TextFieldParser使其变得简单:使用sr作为StreamReader=New StreamReader(fuTheFile.FileContent)Dim csvParser作为New TextFieldParser(sr)csvParser.Delimiters=New String(){,“}csvParser.TrimWhiteSpace=True而不是csvParser.EndOfData Dim fields作为String()=csvParser.ReadFields()'在使用End时对字符串数组End执行某些操作