I'm trying to open a file that includes some data in a web directory that my C# is running. Basically just turns it into a string. I tried doing the following...
string email = File.ReadAllText("/orderforms/email_templates/file_to_include.txt");
I'm not sure if that is the correct method, but it seems there is a pathing problem, the whole path will change depending what web server it is running on.
This is the directory setup...
/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
What path and function should I use to read all of the contents of the file to a string?
Thanks
Try this:
string email = File.ReadAllText(Server.MapPath("~/orderforms/email_templates/file_to_include.txt"))
You need to use Server.MapPath:
http://msdn.microsoft.com/en-us/library/ie/ms524632(v=vs.90).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With