ASP.NET C # retrieves all text from a file in a web path

I am trying to open a file containing some data in the web directory in which my C # is running. It basically just turns it into a string. I tried to do the following ...

string email = File.ReadAllText("/orderforms/email_templates/file_to_include.txt"); 

I'm not sure if this is the correct method, but it seems that there is a problem with the paths, the whole path will change depending on which web server it is running on.

This is a directory setting ...

 /Classes/Page.ascx.cs (the page that tries to read the text from the file) /orderforms/<one of multiple pages execute the above class here or in a sub directory /orderforms/email_templates/file_to_include.txt /orderforms/email_templates/file_to_include2.txt 

Which path and function should be used to read the entire contents of a file in a line?

thanks

+6
source share
2 answers

Try the following:

 string email = File.ReadAllText(Server.MapPath("~/orderforms/email_templates/file_to_include.txt")) 
+24
source

Source: https://habr.com/ru/post/907036/


All Articles