如何使路径可配置?

发布于 2024-11-27 17:58:21 字数 249 浏览 0 评论 0原文

我正在使用以下语法

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

,但我在 c# 代码中包含此语句。我想让路径可配置,例如 在应用程序的 web.config 文件中使用名称路径声明一个键,我应该能够在 C# 代码中获取它 通过 xdocument cofnig =xdocument.Load(path)。

这样可以吗?

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 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2024-12-04 17:58:21

也许做通常的事情就足够了:

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

这假设 web.config 包含:

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

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>
记忆之渊 2024-12-04 17:58:21

我假设您询问如何将键/值对放入 web.config 中,然后在代码中检索它们。请查看以下内容:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文