开发者

How to make a path configurable?

开发者 https://www.devze.com 2023-03-26 04:00 出处:网络
I am using the following syntax XDocument config = XDocument.Load(@\"path to the xml file\"); But i am inlcuding this statement in the c# code.I want to make the 开发者_StackOverflow社区path confi

I am using the following syntax

XDocument config = XDocument.Load(@"path to the xml file");

But i am inlcuding this statement in the c# code.I want to make the 开发者_StackOverflow社区path configurable like declare a key in the web.config file of the application with the name path and i should be able to get that in the c# code by xdocument cofnig =xdocument.Load(path).

Is it possible this way ?


Perhaps it would be sufficient to do the usual:

const string key="xmlPath";
...    
string path = ConfigurationManager.AppSettings[key];
XDocument config = XDocument.Load(path);     

This assumes an web.config which contains:

<appSettings>
  <add key="xmlPath" value="c:\path\to\xml\file.xml" />
</appSettings>


I'm assuming you're asking how to put a key/value pair into the web.config and later retrieve them in the code. Please take a look at the following:

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消