19

The reason I asked this question is that I wanted to create a helper class for Remoting instantiation, and wanted to pass the appropriate app.exe.config (or web.config) file path to the RemotingConfiguration.Configure method, depending on the caller.

Is there a way I could get the name of the config file for both Win and Web apps without checking if the application is Web or WinForms?

3 Answers 3

30

You can use the ConfigurationFile property on the SetupInformation for AppDomain.CurrentDomain.

This will get either the web.config or and app.config (yourprogram.exe.config) location.

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
2
  • 4
    Honestly, this answer strikes me as pretty self-explanatory; the member names are incredibly descriptive.
    – Brian
    Mar 28, 2016 at 19:11
  • 1
    Note this is not supported in .NET Standard 2.0 Jan 13, 2019 at 17:56
14

I've used

string folder = System.Web.HttpContext.Current != null ?
    System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data") :
    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

without problems, but maybe there is some corner cases I don't know about...

1
  • I have capital letter "D" for "App_Data".
    – John
    Mar 27, 2015 at 13:22
3

To find the location of executing application

System.Reflection.Assembly.GetExecutingAssembly().Location;

Don't know about web case.

1
  • I presume you meant System.Reflection.Assembly.GetExecutingAssembly().Location + ".config", but actually I need a solution which would work for both apps.
    – dummy
    Jun 2, 2010 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.