Howto App.config

Topics: CAB & Smart Client Software Factory
Jun 17, 2006 at 10:48 AM
originally posted by: sagi44

Can anyone advise as to what is the best way to add my own application-specific parameters in the app.config file, in the Shell module, and read them in all the other cab modules ?
Jun 17, 2006 at 11:38 AM
originally posted by: sklett

I would create a service in your Common project, then when the shell is adding the default services and other things you would load the values in from the config.

Then you could add aservice dependency in your modules to inject the service from the shell.

As far as how to add items to the config... Google is your friend
Jun 18, 2006 at 7:32 AM
originally posted by: sagi44

I may sound silly, but whats the code for reading a app.config parameter.
I am really confused, since there are so many examples around.
Jun 19, 2006 at 10:58 PM
originally posted by: juemue

In VS 2005, add and open the app.config file to your project.
Create your setting(s) (e.g. MyAppSettingStr).

in code use:
string s = Properties.Settings.Default.MyAppSettingStr;
Jun 19, 2006 at 11:29 PM
originally posted by: KayLerch

In my case, I read out the parameters through ConfigurationsManager to get the ConfigurationSection. In my Shell everything works fine, I get the ConfigSection out of my Shell's app.config. But when I try to access the app.config out of a module in the same way, I still get the ConfigSection of the Shell's app.config.

How can I tell the ConfigurationManager, that I want to access the app.config of the parent module?

The code (it's in the OnRunStarted-method of the main workItem of my module)

ConfigurationSection section = (ConfigurationSection)ConfigurationManager.GetSection("UIElements");
// both, the app.config of my module and the shell have a "UIElements"-section
// the code unfortunetly returns the section of the shell
Jun 26, 2006 at 9:52 PM
originally posted by: KayLerch

does noone have a solution? it's kind of tricky!
Jun 27, 2006 at 12:14 PM
originally posted by: ChrisHolmes

Code for reading from app.config:

-----------------------------------------------------------------------------
AppSettingsReader configReader = new AppSettingsReader();
string address = configReader.GetValue("ServerAddress", typeof(string)).ToString();
-----------------------------------------------------------------------------

Sklett has the right idea - you need to implement a service to read those values, then have your modules use it to read values.
Jul 5, 2006 at 4:38 AM
originally posted by: KayLerch

The solution :

String filePath = this.GetType().Assembly.Location + ".config";

ConfigurationFileMap newAppconfig = new ConfigurationFileMap(filePath);

System.Configuration.Configuration config = ConfigurationManager.OpenMappedMachineConfiguration(newAppconfig);

ConfigurationSection section = config.GetSection("MySection");
Sep 16, 2006 at 3:34 AM
originally posted by: chippong

I was having trouble with this solution because under NUnit my assembly was getting copied to "ShadowCopyCache", but the .dll.config wasn't -- and wasn't being found by Assembly.Location. On advice from Charlie Poole over on the Test Driven Development list (http://www.testdriven.com/modules/newbb/viewtopic.php?topic_id=3992&forum=6) I am switching to Assembly.CodeBase:

// Use Assembly.CodeBase to find the path to the Assembly rather than Assembly.Location
// because sometimes (e.g. when runnning in NUnit), the .dll, but not the .dll.config,
// is copied to "ShadowCopyCache" (a temp directory)

System.Uri codeBaseUri = new Uri(this.GetType().Assembly.CodeBase);

String filePath = codeBaseUri.LocalPath + ".config";