How to insert text from a text file into a label control in C #

Can anyone demonstrate how to embed text from a text file, for example. test.txt in a Label control on a visual C # form please

+7
source share
3 answers

You leave a lot of opinions about where you are right now, and from where you need help, but in the simplest form, try the following:

theLabel.Text = File.ReadAllText(pathToFile); 
+7
source
 label.Text = File.ReadAllText("test.txt"); 

File.ReadAllText Method

+5
source

The answers to the question are still useful.

You are also considering this for WinForms, as I understand from the tags.

If you want to do some kind of action like this in web forms, you have to consider that the text from the file comes without any encoding to control the label. Thus, any JavaScript code can be entered.

Always HtmlEncode your text;

 var pathToFile = Server.MapPath("~/poo.txt"); lblPoo.Text = HttpUtility.HtmlEncode(File.ReadAllText(pathToFile)); 
0
source

All Articles