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.

Comments

blog comments powered by Disqus
Next Post
Giving your Application a voice  24 Nov 2008
Prior Post
Scheduling Downtime  19 Nov 2008
Related Posts
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
Pass implementations, not representations  14 Oct 2017
Avoiding the Singleton Pattern  22 Jul 2017
Implementing the Singleton Pattern  15 Jul 2017
More smart-code posts »
Related Pages
November 2008 archive