Reading a text file into a textBox
-
This doesn't throw any errors, but it also doesn't put the contents of my file into the textBox...
private void ReadFromFile(string\[\] filename) { // create reader & open file TextReader tr = new StreamReader(openFileDialog1.FileName); // read a line of text this.textBox1.Text = tr.ReadLine(); // close the stream tr.Close(); }
Where am I going wrong?
-
This doesn't throw any errors, but it also doesn't put the contents of my file into the textBox...
private void ReadFromFile(string\[\] filename) { // create reader & open file TextReader tr = new StreamReader(openFileDialog1.FileName); // read a line of text this.textBox1.Text = tr.ReadLine(); // close the stream tr.Close(); }
Where am I going wrong?
try tr.ReadToEnd method please. I think the first line is a blank string in your file.
:^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
-
try tr.ReadToEnd method please. I think the first line is a blank string in your file.
:^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
When I saw your suggestion, I got excited. But, alas, there was no happy ending. To put it in context, I'm including the openfiledialog as well.
private void openToolStripButton\_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { this.openFileDialog1.InitialDirectory = @"c:\\"; } } private void ReadFromFile(string filename) { // create reader & open file TextReader tr = new System.IO.StreamReader(this.openFileDialog1.FileName); // read a line of text this.textBox1.Text = tr.ReadToEnd(); // close the stream tr.Close(); }
If you have any more suggestions, I'm all ears.
-
When I saw your suggestion, I got excited. But, alas, there was no happy ending. To put it in context, I'm including the openfiledialog as well.
private void openToolStripButton\_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { this.openFileDialog1.InitialDirectory = @"c:\\"; } } private void ReadFromFile(string filename) { // create reader & open file TextReader tr = new System.IO.StreamReader(this.openFileDialog1.FileName); // read a line of text this.textBox1.Text = tr.ReadToEnd(); // close the stream tr.Close(); }
If you have any more suggestions, I'm all ears.
Soooo....where do you call ReadFromFile???
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Soooo....where do you call ReadFromFile???
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Oops, missed that. Thanks for the catch. Added it...now it just locks up. By the way, did I say or do something wrong???
-
This doesn't throw any errors, but it also doesn't put the contents of my file into the textBox...
private void ReadFromFile(string\[\] filename) { // create reader & open file TextReader tr = new StreamReader(openFileDialog1.FileName); // read a line of text this.textBox1.Text = tr.ReadLine(); // close the stream tr.Close(); }
Where am I going wrong?
If you're using .NET 2.0, System.IO.File.ReadAllText(openfileDialog1.FileName) should do it ( or something similar )
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
If you're using .NET 2.0, System.IO.File.ReadAllText(openfileDialog1.FileName) should do it ( or something similar )
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Thank you so much. I love you , man...
-
Oops, missed that. Thanks for the catch. Added it...now it just locks up. By the way, did I say or do something wrong???
I sugguest you check the value of the openFileDialog1.FileName.Make sure that's a file's real path. If this way cannot help you try these codes: //First ,create a txt file at C:\ // create reader & open file TextReader tr = new StreamReader(@"c:\1.txt"); // read a line of text this.textBox1.Text = tr.ReadLine(); // close the stream tr.Close(); Good luck.
:^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)