36

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?

comecme
  • 6,086
  • 10
  • 39
  • 67

2 Answers2

67

Try AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

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

Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
9
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("~");
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • 1
    That's not what I meant. I am looking for a simple solution where I don't have to know if the application is a windows app or a web app. – comecme May 04 '11 at 19:45
  • Actually, ConfigurationManager should work in both cases, assuming you only read-only access to the root-level web.config. I'll clarify my answer. – Michael Edenfield May 05 '11 at 14:58
  • 2
    Hm. It doesn't work well as I thought. You can use OpenExeConfiguration with the path name override to get at the web.config but it requires having the path to the web site, which kinda defeats the purpose. Forgot I said anything :) – Michael Edenfield May 05 '11 at 15:08