Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all.

I have a text file that I import into my program. My code works fine with a normal text file but the problem is I get text files from a client that for some reason doesn't import properly(It only imports the first line).

If I open the text file in notepad and "save as" a text file and then try to import it again it works fine.

I can't always open the file and save it again as this slows down production and the text file is on average 100mb. So it takes a long time for me to open in notepad and save again.

Is there another way of importing the file?
Here is the code I use:

Dim FILE_NAME As String = TextBox1.Text
Dim objReader As New System.IO.StreamReader(FILE_NAME)
TextBox2.Text = TextBox2.Text & objReader.ReadToEnd
Posted

Are you seriously saying that you are loading 100MB into a text box?
WHY?
Who the heck do you expect to read that? Even an reasonable size .NET manual in PDF format, with pictures, is about a tenth the size. A 100MB text file is approximately 3 million lines of text.
How long is the poor sod being given to read it? A week? A year?

Find a better way: page it for him, provide search facilities, anything but "open a huge file and stuff the user"!
 
Share this answer
 
Try reading it one line at a time inside a loop. like in the example shown here:
http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx[^]

Do not concatenate something like:
s = s & sLine


but use a StringBuilder like:
objStringBuilder.Append(sLine)


Also, if you already know the file size you can use EnsureCapacity to reserve the memory needed.

Good luck!
 
Share this answer
 
Sorry I should have mentioned that no one is going to read the file. We need info from the text file and the program splits up the data and just does a count and wrights a nice small report. The program reads the text file so a user doesn't have to :cool:

The report is just going to say product A = 375 and product B = 645 and so on.

Thanx E.F. Nijboer. I'll have a look at the string builder ;)
 
Share this answer
 
Comments
OriginalGriff 12-Aug-10 8:30am    
Phew! That's a relief!
Have a look at File.ReadAllLines (http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx) which returns an array of strings - but do concatenate with StringBuilder as E.F. suggests.
Thanx everyone I got it working :-D

I had to use a StringBuilder like E.F. Nijboer suggested and then run the data split on the StringBuilder and write the report to a textbox
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900