36

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

tvanfosson
  • 509,016
  • 97
  • 693
  • 791
user23149
  • 543
  • 2
  • 6
  • 13

7 Answers7

50

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

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

will give you the answer.

driis
  • 156,816
  • 44
  • 266
  • 336
27

You can use

AppDomain.CurrentDomain.BaseDirectory;

This gives you the root directory of your application.

pascalhein
  • 5,530
  • 4
  • 30
  • 41
user3866085
  • 271
  • 3
  • 2
23

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

Community
  • 1
  • 1
felickz
  • 4,132
  • 2
  • 32
  • 36
21

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.

MiloTheGreat
  • 710
  • 7
  • 14
12

Best way is using

HostingEnvironment.ApplicationPhysicalPath under System.Web.Hosting

for more information please refer this link

Damith
  • 60,955
  • 13
  • 99
  • 152
9

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/") 
Kjartan
  • 17,961
  • 15
  • 70
  • 90
dove
  • 20,075
  • 14
  • 85
  • 106
0

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

HydPhani
  • 552
  • 5
  • 13