37

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

dove
  • 20,469
  • 14
  • 82
  • 108
user23149
  • 553
  • 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
  • 161,458
  • 45
  • 265
  • 341
32

You can use

AppDomain.CurrentDomain.BaseDirectory;

This gives you the root directory of your application.

pascalhein
  • 5,700
  • 4
  • 31
  • 44
user3866085
  • 321
  • 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,292
  • 3
  • 33
  • 37
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
  • 62,401
  • 13
  • 102
  • 153
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
  • 18,591
  • 15
  • 71
  • 96
dove
  • 20,469
  • 14
  • 82
  • 108
0

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

HydPhani
  • 592
  • 6
  • 13