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
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
In a webservice, you are running in a http context. So,
HttpContext.Current.Server.MapPath("~/")
will give you the answer.
You can use
AppDomain.CurrentDomain.BaseDirectory;
This gives you the root directory of your application.
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.
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/")
HttpContext.Current.Server.MapPath("..") [observe two(..) dots instead of (.)] gives physical directory of Virtual Directory of the site!