0

I have only one app.config inside my project and there is no web.config

It is an excel add-in project and there is only one app.config inside the class library

But when i am trying to access the appSettings, it is not returning values that i have saved in app.config

I tried

ConfigurationManager.AppSettings["KeyName"]

Also during debugging ConfigurationManager.connectionString returns some values whereas there are no connection string that i have saved. I searched the returned value inside the entire solution but no luck

So is there a way where i can identify which is the config file that it is referring to?

P.S I added reference to system.configuration and added that namespace too

adricadar
  • 9,971
  • 5
  • 33
  • 46
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150

1 Answers1

3

ConfigurationManager.AppSettings... will default to the application config file. In this case the application will be Excel. You need to load up the configuration file for your assembly.

var configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
var value = config.AppSettings.Settings["KeyName"].Value;

The app.config file automatically gets renamed to yourassembly.dll.config at build time - hence the need for the second line above. Also note the slightly different code to access the value as we are using the Configuration type here and not the static wrapper exposed directly by the ConfigurationManager.

James Lucas
  • 2,452
  • 10
  • 15
  • Thanks @James !! it is pointing to some dll.config How can i change that dll.config to my app.config? – Vignesh Subramanian Apr 24 '15 at 06:58
  • its placed inside the proJect – Vignesh Subramanian Apr 24 '15 at 06:58
  • You shouldn't need to change it. App.config is what you edit at design time. They are the same just renamed. 'Yourproject'.dll.config is what gets queried at runtime. What is the name of the Excel add-in project you are building? If you edit app.config and rebuild, the 'YourProject.dll'.config file in the build output will have the same content.If it doesn't - probably because you manually edited it so the build won't touch it - then delete it and rebuild. – James Lucas Apr 24 '15 at 06:59
  • its BulkTransfer its pointing to bin/debug/BulkTransfer.dll.config I dont see that file inside debug folder – Vignesh Subramanian Apr 24 '15 at 07:01
  • Ok cool. Edit app.config and rebuild and check the content of BulkTransfer.dll.config. If it doesn't have the edit delete it and rebuild. If you've been editing BulkTransfer.dll.config directly apply those changes to your app.config file so you don't lose them. – James Lucas Apr 24 '15 at 07:03
  • James the bin/debug folder is not containing the BulkTransfer.dll.config file inside it :( There are .dll and .dll.manifest but no .dll.config – Vignesh Subramanian Apr 24 '15 at 07:06
  • I must confess I've not really touched VSTO but this might help with your issue? http://stackoverflow.com/questions/13199432/excel-add-in-not-loading-app-config-with-service-reference-config-information – James Lucas Apr 24 '15 at 07:34