IgorShare Thoughts and Ideas

Consulting and Training

Where is my config file if my main module is not inside the executing folder?

Posted by Igor Moochnick on 09/04/2008

Some people ask me: “What if my bootstrapper is loaded by my application from a another (preconfigured) folder and my Prism/Unity config file located in the same (non-executing) folder? How do I load it?”

It’s pretty simple. You need to take your executing assembly as the base and figure out the required path.

var assembly = Assembly.GetExecutingAssembly();
Uri uriPath = new Uri(assembly.CodeBase);
string path = Path.Combine(Path.GetDirectoryName(uriPath.AbsolutePath), "myApp.config" );
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = path  };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

modulesSection = (ModulesConfigurationSection)configuration.GetSection("modules");

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: