获取当前配置文件的文件名

com*_*cme 34 c# configuration remoting app-config

我认为这很简单,但我找不到答案.

我正在使用远程处理,我想在app.config中存储RemotingConfiguration.当我打电话时,RemotingConfiguration.Configure我必须提供远程信息所在的文件名.所以......我需要知道当前配置文件的名称.

目前我正在使用这个:

System.Reflection.Assembly.GetExecutingAssembly().Location + ".config"
Run Code Online (Sandbox Code Playgroud)

但这仅适用于Windows应用程序,不适用于Web应用程序.

是不是有任何类可以为我提供当前配置文件的名称?

Pau*_*der 63

尝试 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

http://msdn.microsoft.com/en-us/library/system.appdomainsetup.configurationfile.aspx


Mic*_*eld 8

using System.Configuration;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine(config.FilePath);
Run Code Online (Sandbox Code Playgroud)

在奇怪的情况下,您有特定本地或漫游用户的单独配置文件,您也可以使用正确的ConfigurationUserLevel找到它们.对于Web应用程序,请使用

WebConfigurationManager.OpenWebConfiguration("~");
Run Code Online (Sandbox Code Playgroud)

  • 嗯.它不像我想的那样好用.您可以使用OpenExeConfiguration和路径名覆盖来获取web.config,但它需要具有到Web站点的路径,这有点违背了目的.忘了我说了什么:) (2认同)