3

Scenario - I need to access an HTML template to generate a e-mail from my Business Logic Layer. It is a class library contains a sub folder that contains the file. When I tried the following code in a unit test:

string FilePath = string.Format(@"{0}\templates\MyFile.htm", Environment.CurrentDirectory);
string FilePath1 = string.Format(@"{0}\templates\MyFile.htm", System.AppDomain.CurrentDomain.BaseDirectory);

It was using the C:\WINNT\system32\ or the ASP.NET Temporary Folder directory.

What is the best to access this file without having to use an app.config or web.config file?

[This is using a WCF Service]

Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231

4 Answers4

6

You're running this from an ASP.Net app right? Use Server.MapPath() instead.

Also take a look at System.IO.Path.Combine() for concatenating paths.

[Edit] Since you can't use System.Web, try this:

System.Reflection.Assembly.GetExecutingAssembly().Location

Or GetEntryAssembly().

swilliams
  • 48,060
  • 27
  • 100
  • 130
1

I belive you are looking for

System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
1

I got this working on my site! I haven't been working on just this for the last 2 years!!!

I added this inside my system.serviceModel block of the web.config. It seems to make all the paths relative to where your service is installed.

  <system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">

better late than never?

woaksie
  • 89
  • 1
  • 3
  • 13
0

Use

System.Web.HttpServerUtility.MapPath( "~/templates/myfile.htm" )
harpo
  • 41,820
  • 13
  • 96
  • 131