Michael Kniskern
Michael Kniskern

Reputation: 25260

Best way to implement file access in C#

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]

Upvotes: 3

Views: 1394

Answers (4)

woaksie
woaksie

Reputation: 109

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?

Upvotes: 1

swilliams
swilliams

Reputation: 48910

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().

Upvotes: 6

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27374

I belive you are looking for

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

Upvotes: 1

harpo
harpo

Reputation: 43168

Use

System.Web.HttpServerUtility.MapPath( "~/templates/myfile.htm" )

Upvotes: 0

Related Questions