-
May 10th, 2010, 04:36 PM
#1
How to read from a text file into a textbox?
Is this correct way?
StreamReader f = new StreamReader(new FileStream("c:\afile.txt", FileMode.Open, FileAccess.ReadWrite));
char[] d = new char[????????];
f.ReadBlock (d, 0, ??????); - or maybe f.Read()?
textBox1.Text = new string(d);
All MSDN/gooogle examples talk about lines... I want read ALL text, not only one line...
How to learn the length of the text?
-
May 10th, 2010, 05:20 PM
#2
Re: How to read from a text file into a textbox?
StreamReader f = new StreamReader(new FileStream("c:\afile.txt", FileMode.Open, FileAccess.ReadWrite));
textBox1.Text = f.ReadToEnd();
=================
OK! I have found...
But how to write the text back?
Must I create a new file and overwrite old file?
Maybe I can use "FileAccess.ReadWrite" mode?
-
May 10th, 2010, 06:53 PM
#3
Re: How to read from a text file into a textbox?
Try this to read from a file into a text box
Code:
textBox1.Text = File.ReadAllText(@"C:\afile.txt");
and this to write to a file from a text box
Code:
File.WriteAllText(@"C:\afile.txt", textBox1.Text);
and this to find how much text is in the text box
Code:
Int32 length = textBox1.TextLength;
and this to find how big the file is
Code:
FileInfo fileInfo = new FileInfo(@"C:\afile.txt");
Int32 length = fileInfo.Length;
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
May 10th, 2010, 07:46 PM
#4
Re: How to read from a text file into a textbox?
One thing; I would use int instead of Int32. int is an aliased type, so you will never have the potential problems of type mismatches on different platforms (256-bit CPU's? Maybe someday! )
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
May 10th, 2010, 07:49 PM
#5
Re: How to read from a text file into a textbox?
Ahh... thinking about it (and only thinking, not actually going to the effort of actually checking), I beleive the Length property may already be Int64.
So on a 32 bit machine int is 32 bits and on a 64 bit machine int is 64 bits... Is that correct?
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
May 10th, 2010, 08:50 PM
#6
Re: How to read from a text file into a textbox?
Yes, 'int' is an alias to the native integer type on a given platform.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
May 11th, 2010, 04:38 AM
#7
Re: How to read from a text file into a textbox?
wow.. it is so easy! (must be.. I will try it now..).. so easy but so deeply hided..
thank you a lot guys..
I don not need file length in this case..
I was looking it only for char[] d = new char[__?__]; f.ReadBlock (d, 0,__?__);
Seems (to me) I must know it before file loading..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|