Previous     Next

How to access HttpServerUtility.MapPath in a Thread

Usually, when you want the root directory of your website, it is easy to call:

HttpContext.Current.Server.MapPath

or just

Server.MapPath

from a page.

But if you are running your code in a thread or timer, then HttpContext.Current is null (giving you a NullReferenceException), and you are out of luck. :(

However, as long as you are not using virtual directories, there is an easy fix:

AppDomain.CurrentDomain.BaseDirectory

will give you the same result, and doesn’t depend on a current HTTP context.


This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!

Related posts:

  1. We Got Domain - over 10,000 aged domains for sale
  2. See how your website looks in over 60 browsers - in minutes
  3. Easy Cross Domain Cookies (Sharing cookies between domains)


« « Quick Link Building tip
Quick SQL Server Tuning Tip » »

If you liked this, then subscribe to my RSS feed

4 comments

  1. phahar Oct 9

    I am using Virtual Directories. What can I do?

  2. James Oct 9

    This is just what I want. I was looking for a way around this problem for ages!

  3. Gath Oct 9

    phahar - Try something like this:

    ‘Declare this as a global variable
    shared sMapRootDir as string

    ‘Execute this in Application_Start
    sMapRootDir = Server.MapPath(”.”)

    You can then use the variable sMapRootDir throughout your code

  4. capyMoorozy Dec 1

    Hi. I on numerous occasions announce this forum. This is the oldest together unequivocal to ask a topic.
    How multitudinous in this forum are references progressive behind, knavish users?
    Can I worthiness all the advice that there is?

Leave a reply