1

Here is my question, and I have searched this stackoverflow but cannot find the correct solution.

I have an website, that is stored in (locally on my machine)

C:\websites\Website1

in the same folder I have

C:\websites\Website1\xmldoc.xml

I want to be able for the program to find and access the C:\websites\Website1\xmldoc.xml. I have hard coded the C:\websites\Website1\xmldoc.xml location into my website but I am trying to figure out if there is a better way. I have tried Application.StartupPath but it doesn't work.

user990951
  • 1,329
  • 5
  • 24
  • 35
  • A quick search suggests HttpRequest.ApplicationPath. Is that what you are looking for? I believe that is what I have used for the same purpose in the past. – lampej Oct 20 '11 at 20:48

3 Answers3

4

You can use AppDomain.CurrentDomain.BaseDirectory, e.g.

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xmldoc.xml")

This technique is useful in class libraries that can be called from both Web and Windows applications:

  • in a Web application it resolves to the web application directory.

  • in a WinForms or Console application it resolves to the directory containing the executable.

Joe
  • 114,633
  • 27
  • 187
  • 321
2

You can use Server.MapPath to get a local path of a file.

string fullPath = Server.MapPath("~/xmldoc.xml");

See this related SO question and answers.

Community
  • 1
  • 1
Oded
  • 463,167
  • 92
  • 837
  • 979
0

You can use

Request.ApplicationPath

or

Server.MapPath
Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148