Access content file in C # web application

I have this problem on my mind for 3 days.
I have an xml file that is marked as Content and Always Copy .
The file was copied to:
C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\bin\XMLMetadata\Actions.1.xml

When accessing the file:

 //like that: XDocument actions = XDocument.Load("bin\\XMLMetadata\\Actions.1.xml"); //or like that: XDocument actions = XDocument.Load("XMLMetadata\\Actions.1.xml"); //or like that: XDocument actions = XDocument.Load("Actions.1.xml"); 

I get the following exception: Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\IIS Express\bin\XMLMetadata\Actions.1.xml'.

Why was it searched in the IIS folder? how can i access the file?

I am using IIs Express with VWD2010

+4
source share
3 answers

You need to have a relative web application path using

 Server.MapPath("/")+"bin\\XMLMetadata\\Actions.1.xml" 

.

+5
source

Use

 XDocument.Load(Server.MapPath("~/XmlMetaData/Actions.1.xml")); 
+2
source

If the file is static, you might be better off pasting it and using Assembly.GetExecutingAssembly (). GetManifestResourceStream ().

+1
source

All Articles