user990951
user990951

Reputation: 1479

C# Application Path how to get it to work for websites

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.

Upvotes: 1

Views: 5836

Answers (3)

Lajos Arpad
Lajos Arpad

Reputation: 76510

You can use

Request.ApplicationPath

or

Server.MapPath

Upvotes: 0

to StackOverflow
to StackOverflow

Reputation: 124696

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.

Upvotes: 4

Oded
Oded

Reputation: 499002

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.

Upvotes: 2

Related Questions