8

I'm working with DotNetNuke's scheduler to schedule tasks and I'm looking to get the physical file path of a email template that I created. The problem is that HttpContext is NULL because the scheduled task is on a different thread and there is not http request. How would you go about getting the file's physical path?

John
  • 5,381
  • 4
  • 30
  • 26

5 Answers5

14

System.Web.Hosting.HostingEnvironment.MapPath is what you're looking for. Whenever you're using the Server or HttpContext.Current objects, check first to see if HostingEnvironment has what you need.

bdukes
  • 152,002
  • 23
  • 148
  • 175
  • what if you're outside a web project but want to lets say get to the images folder of your web project to grab an image or to read lets say an .htm file's content that sites in your website but you have a utility method you want to call outside to get at that file? – PositiveGuy Jan 27 '12 at 16:50
  • If you're outside of the web project, I would think that you'll need your own custom code to locate the web project anyway. Once you've got that, you can just keep following that path without any special helper like `MapPath`. – bdukes Jan 27 '12 at 22:43
1

There are many ways of doing this, I personally get around it by storing path information as a config option for my modules, it isn't elegant, but it works and works every time.

Joe Brinkman I belive somewhere around has a blog posting on how to construct a new HTTPContext for use inside the scheduler.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • For future readers, the Joe Brinkman articles are [here, for the original version](http://www.dnnsoftware.com/community-blog/cid/135304/the-quest-for-the-dotnetnuke-holy-grail) and [here for an updated version](http://www.dnnsoftware.com/community-blog/cid/137009/new-and-improved-holy-grail). – AaronSieb Nov 18 '14 at 16:53
0

Since this process is really out-of-band in relation to the web site, maybe you can just put the path in a config file.

May not be the best idea, but it is an alternative.

Moose
  • 5,354
  • 3
  • 33
  • 46
0

what says this.GetType().Assembly.Location ?

Michael
  • 9
  • 3
  • 1
    It returns c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\mywebsite\50f1a686\ab5b581d\App_Code.xkcsxvg6.dll – John Sep 20 '08 at 21:29
0

Can you look at the Assembly & the CodeBase paths like this:

Imports System.Reflection
Imports System.IO
...
Path.GetDirectoryName( Assembly.GetExecutingAssembly().CodeBase ) 

That kind of stuff doesn't always work, so what I would recommend doing is writing a log with a bunch of data about the assembly, to see what works in this location. It is what I had to do to get something similar when I was creating a COM component to be hosted in AppCenter. I used this to "get" what "APP_BASE" should be, and set that, so the app.config file would load properly.

Log.Write ( Assembly.GetExecutingAssembly().CodeBase )
Log.Write ( Assembly.GetExecutingAssembly().Location )
Log.Write ( Path.GetFullPath(".") )
Log.Write ( Application.StartupPath )
... and so on, whatever you can think of ...
Andrew
  • 8,322
  • 2
  • 47
  • 70