views:

780

answers:

6

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]

+6  A: 

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
It is being used by a WCF Service.
Michael Kniskern
Hmm, my WCF experience is very limited, do you have access to the Server property? There should be something similar regardless.
swilliams
No, unless I add the System.Web namespace reference
Michael Kniskern
A: 

Use

System.Web.HttpServerUtility.MapPath( "~/templates/myfile.htm" )
harpo
This is actually implemented in a class library file that is reference within the WCF Service. I can not use any System.Web because it is not an ASP.NET application
Michael Kniskern
Just because it's an ASP.NET application doesn't mean you can't use System.Web
John Sheehan
+1  A: 

I belive you are looking for

System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Krzysztof Koźmic
This will only work with the System.Windows.Form namespace. I am using a class library
Michael Kniskern
A: 

On my site none of these work with WCF. All the System.Reflection.Assembly.Get....() methods produce:

Current location = [C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\891551a9\3326dd44\assembly\dl3\7f68b122\f00c611a_984bc901\My.DLL]

Calling location = [C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll]

Executing location = [C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\891551a9\3326dd44\assembly\dl3\7f68b122\f00c611a_984bc901\My.DLL]

System.Reflection.Assembly.GetEntryAssembly() produces a null value.

I am still looking for an answer to this.

woaksie