Get filename of current configuration file

荒凉一梦 提交于 2019-12-03 14:28:31

问题


I'd think this would be simple, but I can't find an answer.

I'm using remoting and I want to store the RemotingConfiguration in the app.config. When I call RemotingConfiguration.Configure I have to supply the filename where the remoting information resides. So... I need to know the name of the current configuration file.

Currently I'm using this:

System.Reflection.Assembly.GetExecutingAssembly().Location + ".config"

But this only works for windows applications, not for web applications.

Isn't there any class that can supply me with the name of the current config file?


回答1:


Try AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

https://docs.microsoft.com/en-us/dotnet/api/system.appdomainsetup.configurationfile




回答2:


using System.Configuration;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine(config.FilePath);

In the odd case that you have separate configuration files for specific local or roaming users, you can locate those as well using the correct ConfigurationUserLevel. For web applications, use

WebConfigurationManager.OpenWebConfiguration("~");


来源:https://stackoverflow.com/questions/5888708/get-filename-of-current-configuration-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!