Load text into textbox (Delphi-style)

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

In Delphi there is the possibilty to load a textfile into a textfield
(memo) without writing too much code. It goes like
memo1.Lines.LoadFromFile('myfile.txt');

What is the C# equivalent (without making use of a streamreader)?

Cheers,

Chris
 
Chris,

You can use the static ReadAllText method on the File class in the
System.IO namespace, like so:

// Set the text in the textbox.
textbox1.Text = File.ReadAllText("filename");

Hope this helps.
 
If I’m not mistaken ReadAllText() is part of the 2.0 Framework, and if you
are using 1.0 or 1.1 you would have to take a step back and do something a
little longer such as:

memo1.Text = File.OpenText("myfile.txt").ReadToEnd();

You are still ultimately dealing with a StreamReader, however a little more
indirectly and inline with Nicholas’s example.

Brendan


Nicholas Paldino said:
Chris,

You can use the static ReadAllText method on the File class in the
System.IO namespace, like so:

// Set the text in the textbox.
textbox1.Text = File.ReadAllText("filename");

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris said:
Hi,

In Delphi there is the possibilty to load a textfile into a textfield
(memo) without writing too much code. It goes like
memo1.Lines.LoadFromFile('myfile.txt');

What is the C# equivalent (without making use of a streamreader)?

Cheers,

Chris
 
Back
Top