Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Put all text from a big file into a textbox
 

Put all text from a big file into a textbox

Hello! I am going to make a text editor and now I want to put all text from a big file (4 MB, 700 000 lines) into a textbox. And it takes very long time to do that! How I make it faster?

The code:
string
 filetext = String.Empty;

StreamReader tr = new
 StreamReader(openFileDialog1.FileName);
filetext = tr.ReadToEnd();
tr.Close();

StringReader sr = new
 StringReader(filetext);

int counter = 0; string line = String.Empty; while ((line = sr.ReadLine()) != null ) { if (line.Length > 0) { textbox1.Value += line; counter++; } }
CheatCat  Saturday, April 11, 2009 12:11 PM

Hi CheatCat,

You can try to use a RichTextBox which support more function than TextBox.

Here is a related topic which may be helpful to you.
http://bytes.com/groups/net-vb/375075-reading-large-text-files-faster

The first reply:
Reading it in all at once is p[robably the "fastest" way, but also the most memory-intensive. Reading it line by line will be slower, but also more "gentle" on your memory usage.

Have you tested my suggestion to use File.ReadAllText method?

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, April 15, 2009 3:00 AM
A useful link

http://www.codeproject.com/KB/files/fastbinaryfileinput.aspx
Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
A.m.a.L - aditi.com - Think Product  Saturday, April 11, 2009 5:24 PM

Hi CheatCat,

Please double click that file and it will be opened by Windows Notepad, does it faster than your application?

Once you try to open a very large txt file with Windows Notepad, it also cost a lot of time to open it. I don't think I can make a program faster than it.

Here I think you can try to use File.ReadAllText method:
textBox1.Text = System.IO.File.ReadAllText(@"C:\a.txt");
To see if it is faster. I have no such large txt file so I cannot do the test for you.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, April 14, 2009 3:11 AM
Notepad is slow, but if I open it with WordPad it go fast. I don't think that a textBox can contain large texts. Also the file I try to read contains lots of ASCII nulls (\0), Notepad and WordPad can print them, but the textBox thinks the text ends after a null..
CheatCat  Tuesday, April 14, 2009 10:32 AM

Hi CheatCat,

You can try to use a RichTextBox which support more function than TextBox.

Here is a related topic which may be helpful to you.
http://bytes.com/groups/net-vb/375075-reading-large-text-files-faster

The first reply:
Reading it in all at once is p[robably the "fastest" way, but also the most memory-intensive. Reading it line by line will be slower, but also more "gentle" on your memory usage.

Have you tested my suggestion to use File.ReadAllText method?

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, April 15, 2009 3:00 AM

You can use google to search for other answers

Custom Search

More Threads

• Problems with My First Whidbey Beta 2 Form
• Keys Property
• Statustrip merge show childforms
• Using embedded resources as background images causes designer errors.
• How can I set the Scrollbar Color for a TreeView?
• Why is a hashtable needed in Dinesh Chandnani's sample XmlDesignerLoader
• Showing User Control Connection initialize error
• Usercontrol customproperties
• pls reply it is urgent! telephone digit limit in textbox, i cant use masked textbox
• display smart tags explicitely