Accessing content file in c# web application

不问归期 提交于 2019-12-12 11:32:01

问题


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


回答1:


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

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

like that.




回答2:


Use

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



回答3:


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



来源:https://stackoverflow.com/questions/7854762/accessing-content-file-in-c-sharp-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!