I am using the following code within a class:
string filePath = HttpContext.Current.Server.MapPath("~/email/teste.html");
The file teste.html is in the folder
But when it will open the file the following error is being generated:
Object reference not set to an instance of an object.
The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
Don't use Server.MapPath. It's slow. Use this instead, HttpRuntime.AppDomainAppPath
. As long as your web site is running, this property is always available to you.
Then use it like this:
string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "email/teste.html");
if the code is not running from within a thread is executing a httprequest
then HttpContext.Current
is null
(for example when you method is called via BeginInvoke
) - see http://forums.asp.net/t/1131004.aspx/1 .
You can always use HttpRuntime
see http://msdn.microsoft.com/en-us/library/system.web.httpruntime.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