04 August, 2008

Custom configuration being loaded by a dll instead of an exe...

If you at times are developing plugins to established applications (e.g. word or excel), you might also be in need of reading configurationvalues for this plugin.
One thing that you do not want is to be forced to have an winword.exe.config to allow the standard ConfigurationManager class to load in these values. Then what does one do...? The System.Configuration.ConfigurationManager is perfectly capable of reading custom configurationfiles provided you supply it with the correct address of the configuration file in question. Here is how:

Create two projects:
1) Console (simulating e.g. winword, excel or other established application)
2) Library (to loadin it's own custom config-settings)

image         image

As seen from the above image - the library project has an app.config file included; this will in turn be renamed Library.dll.config upon compilation in Visual Studio (see image 2). Below is the application using the library that in turn will load in the configuration values. The console below could just as well be e.g. winword.exe.

    class Program
static void Main(string[] args)
Console.WriteLine("Loading external config...");

Library.Class1 c = new Library.Class1();


To load in the configuration values - the Library.Class1 will need to do the following:

    public class Class1
public void WriteAppConfigValues()
string path = Assembly.GetExecutingAssembly().Location;
Configuration cfg = ConfigurationManager.OpenExeConfiguration(path);
var s1 = cfg.AppSettings.Settings["myKey"] != null ? cfg.AppSettings.Settings["myKey"].Value : "None found";

Console.WriteLine("Writing line: {0}", s1);

In this way - you are able to deploy a configuration file for your plugin.

No comments:

iPhone/XCode - not all cases are equal!

This bit me! Having made some changes to an iPhone application (Obj-C); everything worked fine in the simulator. But, when deploying the s...