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?
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?
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;
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! :))
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?
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.
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..