如何通过ConfigurationManager查找配置文件的位置? [英] How to find the Config File location via ConfigurationManager?

查看:71
本文介绍了如何通过ConfigurationManager查找配置文件的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过ConfigurationManager查找配置文件的位置?

How to find the Config File location via ConfigurationManager?

我在代码中有ConfigurationManager类,并且正在调试它.我想知道它指向哪个配置文件(web.config或app.config等).

I have the ConfigurationManager class in code and I'm debugging it. I'd like to know to which config file it's pointing to (web.config or app.config, etc).

ConfigurationManager上是否有任何属性或方法可以帮助您解决此问题?

Is there any property or method on the ConfigurationManager that can help with this?

推荐答案

配置文件本身由

The configuration file itself is represented by Configuration object. To get this object, run this:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

然后,您可以通过 config.FilePath 查看文件路径.

Then you can view the file path via config.FilePath.

更新.正如 Schadensbegrenzer 指出的那样,对于Web应用程序,您将需要另一个代码来加载配置文件:

Update. As pointed out by Schadensbegrenzer for web application you will need another code to load the config file:

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

这篇关于如何通过ConfigurationManager查找配置文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆