user23149
user23149

Reputation: 563

How do I get the current directory in a web service

I am using System.IO.Directory.GetCurrentDirectory() to get the current directory in my web service, but that does not give me the current directory. How do I get the current directory in a web service?

Thanks Stuart

Upvotes: 38

Views: 100546

Answers (7)

dove
dove

Reputation: 20674

HttpContext.Current.Server.MapPath("~/") would get you the root of the application?

Which is plenty most likely as you probably know the path from there.

Another option which might be of interest:

HttpContext.Current.Server.MapPath("/Directory/") 

This builds from the root of the application no matter what.

Without the first slash this will take directory from where you call as the start:

HttpContext.Current.Server.MapPath("Directory/") 

Upvotes: 9

user3866085
user3866085

Reputation: 351

You can use

AppDomain.CurrentDomain.BaseDirectory;

This gives you the root directory of your application.

Upvotes: 35

HydTechie
HydTechie

Reputation: 608

HttpContext.Current.Server.MapPath("..") [observe two(..) dots instead of (.)] gives physical directory of Virtual Directory of the site!

Upvotes: 0

Damith
Damith

Reputation: 63065

Best way is using

HostingEnvironment.ApplicationPhysicalPath under System.Web.Hosting

for more information please refer this link

Upvotes: 12

MiloTheGreat
MiloTheGreat

Reputation: 710

HttpContext.Current.Server.MapPath("~/") maps back to the root of the application or virtual directory.

HttpContext.Current.Server.MapPath("~/") <-- ROOT
HttpContext.Current.Server.MapPath(".") <-- CURRENT DIRECTORY
HttpContext.Current.Server.MapPath("..") <-- PARENT DIRECTORY

All the above is relative, so you can you any combination to traverse the directory tree.

Upvotes: 21

felickz
felickz

Reputation: 4461

HttpContext.Current.Server.MapPath(".") will give you the current working directory.

But to Rohan West's comment about potentially being outside of an HttpContext it would probably be better to just call:

HostingEnvironment.MapPath(".")

See details here

Upvotes: 24

driis
driis

Reputation: 164291

In a webservice, you are running in a http context. So,

HttpContext.Current.Server.MapPath("~/") 

will give you the answer.

Upvotes: 51

Related Questions