43

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.

2
  • Is the call to MapPath where the exception is occurring? It sounds like maybe there is a different line that throws the exception, if that's the case, can you post the line that actually throws the exception. Jul 28, 2011 at 15:18
  • Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. Oct 13, 2013 at 2:57

5 Answers 5

85

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");
8
  • Getting error: "Non-invocable member 'System.Web.HttpRuntime.AppDomainAppPath' cannot be used like a method." while using in class library. Apr 8, 2014 at 5:55
  • Jitendra, HttpRuntime.AppDomainAppPath is a property, not a method. Regards, Nick Apr 8, 2014 at 11:36
  • oh!! its really great worked finally after a longtime
    – Cherry
    Jul 23, 2014 at 11:49
  • @user12345, glad it helped you out. Jul 23, 2014 at 11:51
  • will this work when i deploy my code in server? @nickytonline
    – thiru
    Dec 7, 2016 at 9:50
7

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

7

If there is no HttpContext (e.g. when the method is called via BeginInvoke, as Yahia pointed out), the call to HttpContext.Current.Server.MapPath() must fail. For those scenarios, there's HostingEnvironment.MapPath() in the System.Web.Hosting namespace.

string filePath = HostingEnvironment.MapPath("~/email/teste.html");
1
  • This does not handle relative paths such as ../images/
    – AaA
    Jul 26, 2018 at 8:22
1

You can use something like the following piece of code. One thing to note is that I was facing an issue, where I was trying to access a .txt file from within a TestMethod and everything was failing except for this...and yeah it works for non-Unit Test Scenarios too.

string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,@"..\..") + "\\email\\teste.html";

0

Issue: I had an "Images" folder inside a class library project. But using the above answers, I was not able to get the physical path of the folder to read/write the files inside that folder.

Solution: The below code worked for me to get a physical path in the class library project.

string physicalPath = System.IO.Path.GetFullPath("..\\..\\Images");

I hope, it will help someone who is facing the same issue as me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.