问题描述
如何通过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查找配置文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!