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
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
When are you done?  18 Apr 2022
Fixing GitHub Authentication  28 Nov 2021
Archives
November 2008
2008