Click here to Skip to main content
12,699,308 members (32,876 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
Hi .. I'm working on a project that reads a text file. I open the file using READBLOCK. I get an error when opening big files. No issues when opening a 10MB file but got an error when opening a 1GB file.

Here's the part of the code that opens/reads the file:
Const MAX_BYTES As Integer = 1048576 * 5 '=10 MB 
Dim bytesRead(MAX_BYTES) As Char
Dim numBytesRead As Integer
Dim currentPos As Integer = 0
Dim strFileName As String
Dim strm As System.IO.Stream
Dim TextLine As String
Dim FileDetail As IO.FileInfo

strm = OpenFileDialogMain.OpenFile()
strFileName = OpenFileDialogMain.FileName.ToString()
Using reader As System.IO.TextReader = System.IO.File.OpenText(strFileName)
  numBytesRead = reader.ReadBlock(bytesRead, currentPos, MAX_BYTES)
  TextLine = reader.ReadLine()
End Using

txtEditor.Text = New String(bytesRead)
txtEditor.Text += TextLine
txtEditor.Text = txtEditor.Text & vbCrLf & Now

Does anyone have any suggestion on how to open a large file?

Posted 19-Jun-12 19:48pm
Updated 19-Jun-12 19:49pm
abb_saleh 22-Feb-13 15:10pm
very good tanks
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you want to read it all into memory, a simple File.ReadAllText() will do just fine. If your file is indeed very large, then you can use the StreamReader class, see the below approach. It is sometimes inevitable but should mostly be avoided for style reasons.

im file As New FileInfo("path\to\file")

Using reader As StreamReader = file.OpenText()
    While Not reader.EndOfStream
        Dim nextLine As String = reader.ReadLine()
    End While
End Using

The challenges on large read, is discussed in detail at[^]
sdcruise 20-Jun-12 0:59am
Thanks for the quick response. I tried the "ReadAllText" and "ReadLine" before and I get the same error when opening a large file.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Got a problem with ProcessLine, it says 'ProcessLine' is not declared. It may be inaccessible due to its protection level?
OneInNineMillion 17-Jan-13 3:03am
In response to your question about ProcessLine:
ProcessLine seems to be a custom Sub here.
Crappy example to point out the obvious:

Dim TotalChars As Integer = 0
Private Sub ProcessLine(ByVal LineOfText As String)
TotalChars = TotalChars + LineOfText.Count
End Sub

In this case ProcessLine would help you count the total amount of chars in your file.
Hope that was helpful.
Jared Nathan Drake 21-Nov-14 15:15pm
Hey there, thanks! Will do that :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100