简体   繁体   中英

Accessing content file in c# web application

I am having this issue on my mind for 3 days now.
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 to 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 is it been searched in the IIS folder? how do i access the file?

I am using IIs Express with VWD2010

You need to have web application's relative path by using

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

like that.

使用

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

如果文件是静态的,那么最好嵌入它并使用Assembly.GetExecutingAssembly()。GetManifestResourceStream()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM