Here’s a useful technique for finding the location of configuration files.

Each AppDomain has a base directory from which it loads assemblies – when you double click a WinForms or WPF application, the new process that starts up has an AppDomain whose base directory is the directory in which your exe is located.

This makes it easy to locate any ancillary files that are deployed next to your assemblies:

string domainPath = AppDomain.CurrentDomain.BaseDirectory;
string xmlPath = Path.Combine(domainPath, "Resources\\Controller.Config");
if (File.Exists(xmlPath))
    // Load and process configuration file here

Note that this technique assumes that the AppDomain will have a base directory that points to your application – while this is true for WinForms and WPF applications, “your mileage may vary” with ASP.NET and ASP.NET MVC.


blog comments powered by Disqus