Accessing content file in c# web application

2019-06-21 22:31发布

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

3条回答
在下西门庆
2楼-- · 2019-06-21 23:26

Use

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

查看更多
放荡不羁爱自由
3楼-- · 2019-06-21 23:27

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

查看更多
Anthone
4楼-- · 2019-06-21 23:28

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

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

like that.

查看更多
登录 后发表回答