views:

23

answers:

1

When creating a Visual Studio Add-In, how can you utilise an app.config for the add-in. If I add one to the project and deploy it then when the Add-In runs and I programmatically try to access it via the ConfigurationManager.AppSettings its not picking up the config file for the add-in.
Am I doing something wrong or is there another means to access file based configuration settings for an add-in?

+1  A: 

ConfigurationManager.AppSettings picks up the configuration file for the AppDomain you are loaded into. That config file is typically the one associated with the entry point executable. In your case, you don't control the entry point executable nor the AppDomain you run in so you can't use ConfigurationManager.AppSettings.

You question basically boils down to "How can I have a config file associated with a DLL?" (C# Dll config file). You need to do two things:

  1. Add an Application Configuration File item to your project and make sure you deploy it to the same folder as your DLL.
  2. Access the config file from your DLL using code like this:

     string pluginAssemblyPath = Assembly.GetExecutingAssembly().Location;
     Configuration configuration = ConfigurationManager.OpenExeConfiguration(pluginAssemblyPath);
     string someValue = configuration.AppSettings.Settings["SomeKey"].Value;
    

That will definitely work for regular DLLs that are not loaded using shadow copy. I'm not sure how VS loads its plugins. If you run into problems, let me know and I can post a work around for DLLs that get loaded via shadow copy.

Russell McClure