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
Next Post
Giving your Application a voice  24 Nov 2008
Prior Post
Scheduling Downtime  19 Nov 2008
Related Posts
The Liskov substitution principle goes both ways  21 Mar 2020
Avoid hardcoded wait times  26 May 2018
Guaranteed Progression  19 May 2018
Prefer declarative method names  03 Feb 2018
Using Premeditation  27 Jan 2018
With Relocation  20 Jan 2018
Using Consolidation  13 Jan 2018
Using Semantic Types  06 Jan 2018
Using Extension Methods  30 Dec 2017
Error Methods  25 Nov 2017
More smart-code posts »
November 2008