Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2006

Tip: Looking for answers? Try searching our database.

Getting AppSettingsReader to use app.config file changes at runtime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matt Adamson - 25 Apr 2006 12:12 GMT
Guys,

When writing unit tests for our assemblies we'd like to change configuration
values at runtime for some tests i.e. to test all code paths are executed.
e.g. one method would create a test with a value set to true and the next
test method would set it to false.

I can set the values in the config file easy enough using the following
approach

1)       Get access to the app.config file full path from the process using
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

2)       Load the file into an XmlDocument

3)       Extract the relevant node using code such as  XmlNode node =
xmlDocument.SelectSingleNode(@"/configuration/appSettings/add[@key='" +
configurationName + "']");

4)    Set the value of the "value" attribute using
node.Attributes.GetNamedItem("value").Value =  configurationValue;

However is there any possible way to get the AppSettingsReader class to
reflect the changes at runtime. i.e. so you can execute code such as

AppSettingsReader appSettingsReader = new
System.Configuration.AppSettingsReader();
Boolean value = appSettingsReader.GetValue(key, typeof(Boolean));

and this would reflect the changes.

Any help appreciated

Cheers

Matt
hB - 01 May 2006 06:00 GMT
Better Implement your own Configuration Class. (Config App Block).
Like we normally do as:
class ConfigurationManager : Which reads the config values from one of
the config like just like the app.config and loads all data in the
Class ConfigManager into its properties.

This class can either load data from any xml/txt config file from any
path or registry or any location.
And we have more control and flexibility.

To implement dynamic runtime file changes, for filesystem based file,
implement filesystem watcher in this configmanager class, and have this
class a Load() function which fills up all the properties, and upon
detecting changes just call the Load()

---
hB
Matt Adamson - 02 May 2006 10:00 GMT
Thanks for this although I'm aware of a few alternative solutions I really
wanted to use the existing framework infrastructure for this.

The reason being is I want to avoid changing a lot of existing code just to
get our unit tests working well.

> Better Implement your own Configuration Class. (Config App Block).
> Like we normally do as:
[quoted text clipped - 13 lines]
> ---
> hB

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.