Alois Kraus

blog

  Home  |   Contact  |   Syndication    |   Login
  113 Posts | 8 Stories | 297 Comments | 162 Trackbacks

News



Article Categories

Archives

Post Categories

Image Galleries

Programming

This time I would like to show you the most important changes in the System.Configuration namespace with .NET 2.0.
I have looked at my blog referrer statistics and saw about 20 hits/day by Google. Most of them were searching 
infos how to configure the new Enterprise Library but also a significant number of people which seem to seek guidance to the
following questions:

  • How to read/write to App.Config?
  • How to store a list of objects in a config file via the System.Configuration mechanism?

Reason enough for me to shed more light on the System.Configuration namespace.
The main changes from .NET 1.0/1.1 in the System.Configuration namespace are:

  • Write to your App.Config file through the Configuration class.
  • New configuration model for Windows Forms applications.
  • Store complex objects including object collections in your App.Config File.
  • It is possible to store Connection Strings in the App.Config file see ConnectionSettings this enables you to store you settings on a SQL Server. The Enterprise Library for Sample SqlConfiguration exercises this by implementing a SqlConfigurationSource which can store and retrieve a ConfigurationSection.

So where to start? I think I show you at first the config file and explain how you can create it programtically in your application.

The easiest way to read/write AppSettings

If you want to store only key/value pairs in your App.config file there is a special section in reserved which allows you to do exactly that. Simply add an <appsettings> section and add your data as key/value pairs of the form <add key="xxx" value="xxxx" />. Thats all to create new app.config file with settings in it.

App.Config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="Setting1" value="Very" />

    <add key="Setting2" value="Easy" />

  </appSettings>

</configuration>


The data access API has been changed for this type of setting with .NET 2.0. The "old" one liner ConfigurationSettings.AppSettings  has been deprecated in favor of ConfigurationManager.AppSettings. Beside from the naming change you can now also write your application settings. For read only access you can look at the ShowConfig function defined below. Writing the last modification time is demonstrated in the Main function.

using System;

using System.Collections.Generic;

using System.Text;

using System.Configuration;

 

namespace AppSettings

{

    class Program

    {

        static void ShowConfig()

        {

            // For read access you do not need to call the OpenExeConfiguraton

            foreach (string key in ConfigurationManager.AppSettings)

            {

                string value = ConfigurationManager.AppSettings[key];

                Console.WriteLine("Key: {0}, Value: {1}", key, value);

            }

        }

 

        static void Main(string[] args)

        {

            ShowConfig();

 

            // Open App.Config of executable

            System.Configuration.Configuration config =

              ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 

            // Add an Application Setting.

            config.AppSettings.Settings.Add("Modification Date",

              DateTime.Now.ToLongTimeString() + " ");

 

            // Save the configuration file.

            config.Save(ConfigurationSaveMode.Modified);

 

            // Force a reload of a changed section.

            ConfigurationManager.RefreshSection("appSettings");

 

            ShowConfig();

 

        }

    }

}

Expected Output:

Key: Settings1, Value: Very
Key: Setting2, Value: Easy
Key: Settings1, Value: Very
Key: Setting2, Value: Easy
Key: Modification Date, Value: 01:21:03

With this mechanism you can read and update simple key/value pairs within your application without digging any deeper in the System.Configuration namespace. The following examples show the other features like the new windows forms configuration mechanism, create your own configuration section and how you can easily store lists of objects in the App.config file with the new Enterprise Library helper classes.


Windows Forms Configuration
When developing Windows Forms with VS2005 you get for free a new configuration mechansim. They Forms designers were so nice to create from your config values automatically an access class and came up with an consistent model to store application global config files in the app.config.exe file and user specific settings within the user profile in user.config.
Please note that the Forms configuration model is not available in class library projects since you have no App.config file for your Dll. When you add a settings file to your class library project you can and merge the settings with the App.config  file of your hosting executable. This can be useful if you want to enforce that every application that uses your library can have its own settings inside the App.config file of the executable. You have the freedom to store your settings where ever you would like to. Any provider can be plugged into your config data access class by decorating your configuration class with the SettingsProviderAttribute. If none is specified the LocalFileSettingsProvider is used which relies on the System.Configuration. This is the reason why you do not need to reference the System.Configuration assembly in a windows forms but you see the System.Configuration assembly loaded in your windows forms application. You can check it with the debugger in the loaded modules list.
Below is a new Windows Forms project shown  which was generated via New->Project->Windows Application. The new configuration features are visible in the Properties folder of your project. There go your resources and the automatically generated strongly typed resource access class with static properties to allow easy and type safe access to your resources. This is similar to the old C programming model with windows resources. You had an header file with resource ids generated by the resource compiler which spits out an header file which is compiled (compile time checking of the existence of resources) and an object file which is linked into you target. Now you have also compile time checking in .NET if you access your resources via the static properties.
The configuration features surface in the auto generated Settings.settings file and Settings.Designer.cs. 
To create new configuration values you have full Designer integration within Visual Studio (see picture below). In your code you can read/modify these settings via the generated access class. Inside the visual editor you can choose between two scopes for each of your configuration settings: Application and User. The Application scope defines configuration values which cannot be changed by the user and are the same for all users of this application. User scoped settings on the other hand can be changed/created by, well the users and are stored within their local profile. Addendum: Application scoped settings cannot be altered when you save your settings. Only the user settings are written to disk during a save operation!



VS 2005 generated Windows Forms application skeleton.

User Specifc Application Settings



Settings.settings 
(Generated by Visual Studio)

                <?xml version='1.0' encoding='utf-8'?>

                <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WindowsApplication1.Properties" GeneratedClassName="Settings">

                  <Profiles />

                  <Settings>

                    <Setting Name="testSetting" Type="System.String" Scope="User">

                      <Value Profile="(Default)">Form1</Value>

                    </Setting>

                  </Settings>

                </SettingsFile>




Settings.Designer.cs (Generated by Visual Studio using the SettingsSingleFileGenerator as Custom Build Tool)

    namespace WindowsApplication1.Properties 

    {

       internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 

       {

       

          private static Settings defaultInstance = 

            ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

       

          public static Settings Default 

          {

            get 

            {

                return defaultInstance;

            }

          }

       

          [global::System.Configuration.UserScopedSettingAttribute()]

          [global::System.Configuration.DefaultSettingValueAttribute("Form1")]

          public string testSetting 

          {

             get 

             {

                 return ((string)(this["testSetting"]));

             }

             set 

             {

                 this["testSetting"] = value;

             }

          }

      }

    }


 


To load your settings programtically you only need to to  do a

            Settings set = Settings.Default;

and access your settings via the property of the returned instance.

            string str = set.testSetting;

Wow that was easy. Wasn´t it? Now lets save our changed test setting:

            set.testSetting = "test value";

            set.Save();

That's pretty much it. To display some of your settings in your form you can use data binding and let your users configure the application font, color, .... User specific settings are stored in  %APPDATA%\<AppName>\<AppName><AppConfigName_GUID>\<AssemblyVersion>\user.config. The path to the user config is on my machine is e.g. %APPDATA%\WindowsApplication1\WindowsApplication1.exe_Url_x00ebzylso3e0rtwd1pnxkhduwr34pgb\1.0.0.0. This enables you to install a Windows Forms App as Administrator for all users with some global settings in the executable App.config and user specific settings which are stored in the user profile. If your users are in a domain with a roaming profile they will get the same profile and thus the same user settings on every computer they work.

Is this new mechanism compatible with the old one?

Yes it is. Even more: These nice classes do rely heavily on the System.Configuration features. Each user/application section is put into its own ConfigurationSectionGroupCollection which can be accessed programtically. Every group does contain one or more configuration section/s of the type ClientSettingsSection which does serve as container for your strongly typed key/value pairs. The following code does enumerate all your auto generated settings and does print them out to the Console.


            // Get the application configuration file.

            System.Configuration.Configuration config =

                    ConfigurationManager.OpenExeConfiguration(

                    ConfigurationUserLevel.None);

 

            // Get the collection of the section groups.

            ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

            // Show the configuration values

            ShowSectionGroupCollectionInfo(sectionGroups);

 

        static void ShowSectionGroupCollectionInfo(ConfigurationSectionGroupCollection sectionGroups)

        {

            ClientSettingsSection clientSection;

            SettingValueElement value;

 

            foreach (ConfigurationSectionGroup group in sectionGroups)  // Loop over all groups

            {

                if (!group.IsDeclared) // Only the ones which are actually defined in app.config

                    continue;

 

                Console.WriteLine("Group {0}", group.Name);

 

                foreach (ConfigurationSection section in group.Sections) // get all sections inside group

                {

                    clientSection = section as ClientSettingsSection;

                    Console.WriteLine("\tSection: {0}", section);

                    if (clientSection == null)

                        continue;

 

                    foreach (SettingElement set in clientSection.Settings)

                    {

                        value = set.Value as SettingValueElement;

 

                        // print out value of each section

                        Console.WriteLine("\t\t{0}: {1}",set.Name,value.ValueXml.InnerText);

                    }

                }

            }

        }


How To Read/Write Another App.Config File
To open another App.Config file you need to create an instance of ExeConfigurationFileMap. The purpose of this class is not that obvious but we can use it to open another file. Once you have learned this little trick the rest is easy. Here is a little example that does open an file by specifying it's name.

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

            fileMap.ExeConfigFilename = @"ConfigTest.exe.config"// relative path names possible

            // Open another config file

            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

 

            // read/write from it as usual

            ConfigurationSection mySection = config.GetSection("mySection");

            config.SectionGroups.Clear(); // make changes to it

            config.Save(ConfigurationSaveMode.Full);  // Save changes


The Microsoft Enterprise Library way has a shorthand utility class for this. It is the FileConfigurationSource which does hide those strange things. Tom Hollander has a nice post explaining this already so I will not repeat the same at my blog.

Another Way to read/write configuration values

A more advanced way to store our settings is to create our own ConfigurationSection. This makes our configuration values distinguishable from other configuration values inside the App.config file. It is a little more complicated since you have to write your own class which content is de/serialized to the App.config file. I am going to show you at first the config file and explain then what code you need to write to read/save these settings to your application configuration file.

App.Config  (Taken from the Enterprise Library Configuration Migration QuickStart Sample)

<configuration>

  <configSections>

    <section name="EditorSettings" type="ConfigurationMigrationQuickStart.EditorFontData, ConfigurationMigrationQuickStart, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" />

  </configSections>

  <EditorSettings name="Verdana" size="24" style="2" />

</configuration>

Most App.config files which contain config data have a <configSectionselement where many <section> are defined. The name attribute of an section (in this example "EditorSettings") tells the config system that the class ConfigurationMigrationQuickStart.EditorFontData is responsible for the ser/deserialization of the node <EditorSettings>. The EditorFontData class derives from the ConfigurationSection class and uses the ConfigurationProperty attribute to create a mapping between the properties to de/serialize and the attribute names in names in the App.Config file.

      using System.Text;

      using System.Configuration; 

      public class EditorFontData : ConfigurationSection

      {          

 

            public EditorFontData()

            {         

            }

 

            [ConfigurationProperty("name")]

            public string Name

            {

                  get { return (string)this["name"]; }

                  set{ this["name"] = value; }

            }

 

            [ConfigurationProperty("size")]

            public float Size

            {

                  get{ return (float)this["size"]; }

                  set{ this["size"] = value; }

            }

 

            [ConfigurationProperty("style")]

            public int Style

            {

                  get { return (int)this["style"]; }

                  set{ this["style"] = value; }

            }

      }

To access an EditorFontData instance with values from your config file you only need to call

   EditorFontData configData = ConfigurationManager.GetSection("EditorSettings") as EditorFontData;

Please note that the System.Configuration.ConfigurationManager returns only objects with read only properties. Subsequent calls to GetSection use the cached instance inside the ConfigurationManager. This constraint requires you to create every time a new instance of you e.g. EditorFontData object if you want to write to the App.config file. You even cannot add an object with the same name twice to the System.Configuration.Configuration object. Now comes the fun part: To edit an existing App.config file you have to remove your config object and then add a new object instance to the Configuration object. Only then the Save will succeed.

                EditorFontData configData = new EditorFontData();

                configData.Name = "Arial";

                configData.Size = 20;

                configData.Style = 2;

               

                // Write the new configuration data to the XML file

                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                config.Sections.Remove("EditorSettings");

                config.Sections.Add("EditorSettings", configData);

                config.Save();


To get your hands on the System.Configuration.Configuration object you have to open your App.Config file. The .NET config mechanism supports setting inheritance from the Machine.config from which all settings are inherited. Next comes the App.Config file which is selected by the ConfigurationUserLevel.None file.




Enterprise Library Configuration Helpers

The Enterprise Library for .NET 2.0 has made it a lot easier to use the System.Configuration classes.More info how to configure the new application blocks you can  be found in another article. Lets suppose you want to store a collection of strongly typed named objects in our App.Config file. In this example it is a plugin configuration file where a list of plugins can be added and the selected plugin be started. The config file could look like this:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="PluginConfiguration" type="EntlibInjection.PluginManagerConfiguration, EntlibInjection, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />

  </configSections>

 

  <PluginConfiguration name="Configured Plugins" SelectedPlugin="2D">

    <Plugins>

      <!-- type="EntlibInjection.PluginData, EntlibInjection, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" -->

      <add name="2D" cfgfile="2D.config" plugintype="EntlibInjection.TwoD, EntlibInjection, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" type=""/>

      <add name="3D" plugintype="ThreeDPlugin.ThreeD, ThreeDPlugin, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" type=""/>

    </Plugins>

  </PluginConfiguration>

 

</configuration>


We want a PluginConfiguration class which selects a plugin and contains a list of plugins. To make it work we first need to define the PluginManagerConfiguration class. We need the Enterprise Library  only for the IConfigurationSource interface to load our config from other files than App.config or even an SQL server.

PluginManagerConfiguration.cs

using System;

using System.Configuration;

using System.ComponentModel;

using System.Collections.Generic;

using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

 

namespace EntlibInjection

{

    public class PluginManagerConfiguration : SerializableConfigurationSection

    {

            private const string selectedPlugin = "SelectedPlugin";

            private const string PluginCollectionProperty = "Plugins";

            private const string nameProperty = "name";

 

 

            /// <summary>

            /// Configuration section name for Plugin Configuration

            /// </summary>

            public const string SectionName = "PluginConfiguration";

 

 

            public PluginManagerConfiguration()

            {

 

            }

 

            /// <summary>

            /// Gets the configured plugin settings section in the configuration source.

            /// </summary>

            /// <param name="configurationSource">The <see cref="IConfigurationSource"/> to get the section from.</param>

            /// <returns>The plugin configuration.</returns>

            public static PluginManagerConfiguration GetPluginSettings(IConfigurationSource configurationSource)

            {

               return (PluginManagerConfiguration)configurationSource.GetSection(PluginManagerConfiguration.SectionName);

            }

 

            /// <summary>

            /// Get/Set the selected plugin

            /// </summary>

           [ConfigurationProperty(selectedPlugin, IsRequired = true)]

            public string SelectedPlugin

            {

                  get

                  {

                     return (string)this[selectedPlugin];

                  }

                  set

                  {

                     this[selectedPlugin] = value;

                  }

            }

 

        /// <summary>

        /// Name of the configuration node.

        /// </summary>

        [ConfigurationProperty(nameProperty)]

        [EditorBrowsable(EditorBrowsableState.Never)]

        public string Name

        {

            get

            {

                return (string)this[nameProperty];

            }

            set

            {

                this[nameProperty] = value;

            }

        }

 

            /// <summary>

            /// This property contains a list of plugins

            /// </summary>

            [ConfigurationProperty(PluginCollectionProperty)]

            public PluginDataCollection Plugins

            {

                  get

                  {

                      return (PluginDataCollection)base[PluginCollectionProperty];

                  }

            }

 

    }

}


After the definition of the Configuration Section we need the collection class which contains a list of elements. If you want a list of strongly typed data elements in the config file you can use the PolymorphicConfigurationElementCollection<T> generic which is introduced by the Enterprise Library. This class was specifically designed to store lists of the following form:
<SectionName>

      <add name="blah" type="PluginData strongly typed " xxx="xxxx" xxx="xxxx" .../>

</SectionName>


You have to derive from PolymorphicConfigurationElementCollection and implement the RetrieveElementConfigurationElementType where you retrieve the data type which is responsible for the ser/deserialization of the other attributes of this node. Error handling code has been omitted. Please have a look at e.g. the Logging Application Block of Entlib to see the correct error handling.
 
PluginDataCollection.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

 

 

namespace EntlibInjection

{

    public class PluginDataCollection : PolymorphicConfigurationElementCollection<PluginData>

    {

            /// <summary>

            /// Returns the <see cref="ConfigurationElement"/> type to created for the current xml node.

            /// </summary>

            /// <remarks>

            /// The <see cref="PluginData"/> include the configuration object type as a serialized attribute.

            /// </remarks>

            /// <param name="reader"></param>

            protected override Type RetrieveConfigurationElementType(XmlReader reader)

            {

                 return typeof(PluginData);

            }

 

    }

}

I have simplified the RetrieveConfigurationElementType a lot compared to the normally used strongly typed name which should be specified to allow versioning of you configuration node data types. Please do NOT use this shortcut in production environments. If you do not want strong names for your data types then the PolymorphicConfigurationElementCollection<xxx> is not the right choice for your problem. Instead a direct derivate of ConfigurationElementCollection is the better alternative for you. Do not ask me how to figure out with which version of PluginData you App.Config was created when you do not use strong names for your data types.This can be an important issue if you plan e.g. to create configuration converters from one version to the next. Please have a look at the Logging Block code e.g. TraceListenerDataCollection how the PolymorphicConfigurationElementCollection<xxx> is correctly implemented. Ok so much for versioning. Now we can create our PluginData class which contains the plugin data type and an external config file which contains the plugin configuration. If it is not specified we assume the the plugin configuration is located in the App.config file. This can be  implemented by switching from SystemConfigurationSource to FileConfiguratonSource and put the plugin config file into it.

 PluginData.cs

using System;

using System.ComponentModel;

using System.Collections.Generic;

using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

using System.Configuration;

 

 

namespace EntlibInjection

{

    public class PluginData : NameTypeConfigurationElement

    {

        public const string PluginTypeProperty = "plugintype";

        public const string PluginConfigProperty = "cfgfile";

 

 

          private static IDictionary<string, string> emptyAttributes = new Dictionary<string, string>(0);

 

     

            public PluginData()

            {

            }

 

            protected PluginData(string name, Type PluginType)

                  : base(name, PluginType)

            {

                this.PluginType = this.GetType();

            }

 

 

 

            [ConfigurationProperty(PluginConfigProperty, DefaultValue="", IsRequired = false)]

            public string ConfigFile

            {

                  get

                  {

                     return (string)this[PluginConfigProperty];

                  }

                  set

                  {

                     this[PluginConfigProperty] = value;

                  }

            }

 

 

            [ConfigurationProperty(PluginTypeProperty, IsRequired= true)]

            [TypeConverter(typeof(AssemblyQualifiedTypeNameConverter))]

            public Type PluginType

            {

                  get

                  {

                        return (Type)this[PluginTypeProperty];

                  }

                  set

                  {

                        this[PluginTypeProperty] = value;

                  }

            }

    }

}

 

To load the configuration form a config file you can use the FileConfigurationSource from the Enterprise Library and you are ready to rockn roll.

         FileConfigurationSource configurationSource = new FileConfigurationSource(“2D.config”);

         PluginManagerConfiguration.GetPluginSettings(configurationSource);

There are many new things within the System.Configuration namespace which this article tries to show you how the can be used. I hope this article gives you enough starting points to jump up on the .NET 2.0 wagon and the new Enterprise Library. One last thing: User settings are not supported by the Enterprise Library. But you can fake this by creating a config file in %APPDATA%\MyEntlibApp\<MyEntlibApp Assembly Version>\User.config and load it from there. To make this work you need a loaded user profile which is not always present e.g.  System Service. In this case you need to load some trusted user profile to make it work.


 

posted on Wednesday, January 4, 2006 10:55 PM

Feedback

# re: Read/Write App.config with .NET 2.0/Enterprise Library 1/25/2006 12:17 AM Daniel Linyu
Greate job! Very clear!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 2/1/2006 2:52 PM Shaun
What a great post. Most appreciated.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 2/5/2006 6:04 PM Richard Tang
Alois,
Very good article.
Is that possible to have elements or collections within a collection element? I happen to have a exising configuration file with nested collections in the collections.
Thanks!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 3/21/2006 5:22 PM Draco
Great Post!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/4/2006 10:24 PM SB
Great article, answered a lot of my questions. One question, I have left is trying to access application settings created in one project from another project. I created ConnectionString var in my start up project, but I need to access the ConnectionString var from another project in the same solution. Doesn't seem like I can do this. If I do it the other way and create the ConnectionString var in the project that I need it for, the ConnectionString var no longer appears in the AppConfig.exe file when I compile my application. Any ideas on how to access app settings from another project?
Thanks

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/5/2006 8:19 PM Alois Kraus
If you mean other project you mean App.Config of another applicaton? It is possible to refer from one section to another App.Config file by using the configSource Attribute. More info can be found at MSDN and Tom Hollander Blog: http://blogs.msdn.com/tomholl/archive/2006/04/02/entlib2externalconfig.aspx

Yours,
Alois Kraus


# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/12/2006 12:13 PM Gilles Prod'Hon
Hello,

How can we do the RefreshSection on app.config that is not the config file of the Start Up Project.

My problem is that I have 3 projects One defined as the Start Up project and the two others are library, and my connectionString is in a library and what I can say is the ConfigurationManager.RefreshSection does not working on it. If you can just explain how we dow the refresh on the library.

Thank you.

Gilles

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/25/2006 7:17 PM SB
My problem is: I have several projects within my solution. I create the AppConfig file in my startup project. However, if I try to access any settings in the AppConfig file, for example ConnectionString setting, from any project that is not the startup project I am not able to. It seems the settings are now out of scope. How can I access the settings in the AppConfig file from a project that is not the start up project? Thanks.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/28/2006 9:06 PM Alois Kraus
Hi,

to open another app.config file on the fly you can do the following:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = fileName;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

// Now you can do a
config.GetSection,
Add/RemoveSection
and
config.Save

as you like.

HTH,

Alois Kraus



# re: Read/Write App.config with .NET 2.0/Enterprise Library 5/14/2006 3:39 PM Jude Wong
Hi Alois,

I'd searched for a long time information (samples of course!) on the configuration classes in EL 2.0.. all in vain until I found this blog!

Two thumbs up! Sorry I only have two hands!! :-)

# re: Read/Write App.config with .NET 2.0/Enterprise Library 6/2/2006 10:38 AM John Radley
Hi
If I don't know the Database name (for example) until run-time (when the User choses it from a List), how can I ensure that all the various Enterprise Libs, Membership providers get to use the new, correct ConnectionString?

Of course, I can write back to the app.config. But in my case the App runs on Terminal Services, and so I would have a problem with multiple users.

What I would like to do is have a Configuration String Provider, which all Libs use. But it does not seem that System.Configuration.Providers is intended for ths. Am trying to use a Protected Config String provider as all Libs will use the Decrypt method.
Do you have any other suggestions?
Thanks
John


# re: Read/Write App.config with .NET 2.0/Enterprise Library 6/2/2006 9:40 PM none
Hi John please drop me a mail so I can save some space in my comments section ;-)

# re: Read/Write App.config with .NET 2.0/Enterprise Library 7/6/2006 7:01 PM Alois Kraus
Hi Steve,

I have updated my article to answer your question.

Yours,
Alois Kraus


# Donde esta el Application Block de Configuracion en Enterprise Library 2.0 ??? 8/16/2006 2:57 AM ElBruno
Esta pregunta me la han hecho varias veces, desde el lanzamiento de Enterprise Library 2.0 , y la respuesta

# re: Read/Write App.config with .NET 2.0/Enterprise Library 8/16/2006 3:00 PM Eugene
Hi Alois,
I need your help badly.

I'm using .NET 2.0.
I've included "using System.Diagnostics;"
When I try using "ConfigurationManager.AppSettings"
I'm getting following error:
"Error 8 The name 'ConfigurationManager' does not exist in the current context "C:\....\Test.cs".

At the end I took your code and I'm having same problem :-(

I can not find ConfigurationManager in System module via Object Browser.

Any ideas?
Thanks,
Eugene.


# re: Read/Write App.config with .NET 2.0/Enterprise Library 8/16/2006 3:29 PM Chris
I am having the same problem as Eugene. I cannot select a ConfigurationManager from anywhere.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 8/26/2006 11:11 AM Lars
You need to add the system.configuration reference in the project . He should have mentioned this since it is not part of the standard.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 9/21/2006 2:07 PM MicheleB
Hi Alois, very good article!

Inspired from it, I'm trying to write a proc that stores user settings in app.exe.config insted user.config. I will use it in substitution of Properties.Settings.Default.Save(). Here the code:

static void SaveUserSettings()
{
ClientSettingsSection clientSection;
SettingValueElement value;

System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

ConfigurationSectionGroup group = config.SectionGroups["userSettings"];
if (group == null)
throw new InvalidOperationException("Gruppo userSettings non presente nel file di configurazione");

foreach (ConfigurationSection section in group.Sections)
{
clientSection = section as ClientSettingsSection;
ClientSettingsSection newSection = new ClientSettingsSection();

if (clientSection == null)
continue;

foreach (SettingElement set in clientSection.Settings)
{
value = set.Value as SettingValueElement;
value.ValueXml.InnerText = Properties.Settings.Default[set.Name].ToString();
newSection.Settings.Add(set);
}

config.Sections.Remove("userSettings");
config.Sections.Add("userSettings", newSection);
}

config.Save(ConfigurationSaveMode.Full);
}


So far, so good. The proc works properly on strings but, with some types (eg. Font) the string written in the file is in a different format than the original

Setting writed with Properties.Settings.Default.Save():

<setting name="Test" serializeAs="String">
<value>Arial, 11.25pt, style=Bold</value>


Setting writed with my proc:

<setting name="Test" serializeAs="String">
<value>[Font: Name=Arial, Size=11, Units=3, GdiCharSet=1, GdiVerticalFont=False]</value>


...i suppose the problem is in the line:

value.ValueXml.InnerText = Properties.Settings.Default[set.Name].ToString();


Can you help me?!?!
Thanks in advance
Michele

# re: Read/Write App.config with .NET 2.0/Enterprise Library 9/23/2006 6:59 AM Alois Kraus
Hi Michele,

this is not so easy since there seems to go some magic in the background going on to find out the correct type do deserialize from

<setting name="s4" serializeAs="String">
<value>Microsoft Sans Serif, 8.25pt</value>
</setting>
I think you will need to do reflection on the accessor class to find for each property name the returned type and with this knowledge the corresponding settings name inside your App.config. It is a bit more work but if you want to save in a generic way this would be the way to go. If this is worth the effort is another question.

Yours,
Alois Kraus



# ¿Donde esta el Application Block de Configuracion en Enterprise Library 2.0? 9/30/2006 5:01 AM El Bruno
Esta pregunta me la han hecho varias veces, desde el lanzamiento de Enterprise Library 2.0 , y la respuesta

# How to Read / Write App.config with .NET 2.0 10/11/2006 5:51 PM Clayton Firth's Blog

Thanks to Alois Kraus. Great content on reading and writing config files using .NET 2.0

Read/Write...

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/1/2006 3:19 AM Michael Freidgeim
To update key in AppSetings you need to Remove and then Add key.
I've posted the helper method http://geekswithblogs.net/mnf/archive/2006/11/21/97693.aspx

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/5/2006 2:11 AM Darrell Plank
Very good post. One thing I'm still wondering about is how to save XML data. I've been saving in an XML file, so all the data is already in an XmlDocument. Surely, if anything, this ought to be easier than de/serialing a binary object into XML and then saving it.

Thanks for any advice.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/5/2006 5:03 PM Darrell Plank
In the previous post, I said, regarding putting an XmlDocumnt into the config file "surely, if anything, this ought to be easier than de/serialing a binary object into XML and then saving it". I've read more and am pretty much concluding that in spite of the fact that this seems to make sense, the reality is the precise opposite - instead of being simple, it's actually impossible to directly place an XmlDocument/XmlNode into the config file.

Which brings up a much larger question for the community at large regarding the design for this configuration stuff. Isn't all of this solely for the purpose of putting some arbitrary XML into app.config? And don't they already have great facilities for producing XML? So why this totally new design to turn a type into XML solely for the configuration file? Why didn't they just give us a simple "SaveXmlToConfigFile(string appName, XmlNode node)"? Wouldn't this single, simple call have replaced all of their new design? And if this is such a great way to put XML into a file, why don't they make it apply to files in general rather tha restricting it to config files? Is there some hidden capability that I'm unaware of that only applies to the XML in application sections of config files which this design somehow caters to? And if you want a design that turns a structure into XML, isn't that precisely what XML serialization is all about? Why wasn't that used/expanded?

I'm a little confused as to the motivation behind all this seemingly needless complexity. Is there some explanation for it that somebody is aware of?

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/19/2006 4:34 AM Mark Vandersteen
G'day,
Hi hope you can help,

We have recently started using Microsoft Enterprise Library and I'm trying to completely separate the database settings from my app by placing standard database functions & ldap directory lookup functions into a class.library.
This is to central all our standard routines etc into one place. This is fine and seems simple enough.

Problem arrises using Microsoft Enterprise Library & app.config within the class.library.

I can place all application settings into the app.config file within the class.library and these settings can be lookup and accessed without issue ie. LDAP_HOT_NAME and these sort of variable/settings. The problem comes when trying to add in database connect string via Enterprise Library Configration tool. If the database setting are created through here and saved into the app.config file I end up not being able to access the connection string, but can still grab the application settings etc. If i move the connectionstring items into the web.config file of the actual program (the program using the class.library) then the connectionstring can be found.

Is there anything to be aware off when trying to "centralise" these connectionstrings into the app.config of a class.library, or will Microsoft Enterprise Library only use the web.config file from the main program.

Hope this all make sense
Mark

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/21/2006 12:06 AM Alois Kraus
Hi Mark,

the ConnectionsStrings property is a special one which is "hard" coded into the .NET App.Config schema like AppSettings. You do not need to create a special section for them and you can access you connection strings very easily via the ConfigurationManager.ConnectionStrings collection.
How did you access the connection string in your windows forms applicaton?

Yours,
Alois Kraus


# re: Read/Write App.config with .NET 2.0/Enterprise Library 1/5/2007 11:13 AM Anjan
How to edit the appSettings through Enterprise Library Configuration Tool v2.0?

Thanks
Anjan

# re: Read/Write App.config with .NET 2.0/Enterprise Library 1/18/2007 3:48 PM Oliver Hausler
Hi Alois,

Many thanks for your really great article. I've been searching for days for information about this not very well documented part of dotnet.

My problem is the following: I am trying to read the app.config from a DLL (which is part of the application but not called from the application), because the DLL does not automatically read the app.config. When the DLL is called by an external thread, I want it to use the same configuration values, e.g. for tracing, like the EXE.

Thanks to your article I managed to read the app.config by using the following code:

Dim FileMap As New ExeConfigurationFileMap
FileMap.ExeConfigFilename = "app.config"
Dim Configuration As Configuration = ConfigurationManager.OpenMappedExeConfiguration(FileMap, ConfigurationUserLevel.None)
Dim SystemDiagnostics As ConfigurationSection = Configuration.GetSection("system.diagnostics")

But then I am stuck with lots of objects and elements which I would have to retrieve manually and load it onto the appropriate objects, e.g. TraceListeners.

I found the following framework class, which might do my whole job with one command, but Microsoft states it is deprecated.

DiagnosticsConfigurationHandler()

Do you know more or have an idea? I would really appreciate your help.

Oliver.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 1/29/2007 4:56 PM Waldy
Eugene wrote:

>I'm using .NET 2.0.
>I've included "using System.Diagnostics;"
>When I try >using "ConfigurationManager.AppSettings"
>I'm getting following error:
>"Error 8 The name 'ConfigurationManager' does >not exist in the current >context "C:\....\Test.cs".

Are you sure you should be programming?!! That error is like basic C# programming for beginners, day 1.


# re: Read/Write App.config with .NET 2.0/Enterprise Library 1/29/2007 7:35 PM Alois Kraus
Hi Waldy,

please note that
using System.Configuration;
and adding a reference to the System.Configuration assembly will fix your problem. System.Diagnostics will not help you to get a running program. A good way to find such things out is MSDN.

Yours,
Alois Kraus


# re: Read/Write App.config with .NET 2.0/Enterprise Library 2/13/2007 4:31 PM vick
Hi,
My solution has many projects, each one has a app.config file, which are correct.
However when i look at me solution.exe.config file, some values are out of date...


How do I resync the solution.exe.config file to contain all the correct values from the app.config files?

# re: Read/Write App.config with .NET 2.0/Enterprise Library 2/13/2007 11:50 PM Alois Kraus
Hi vick,

could you elaborate a bit what you mean with out of date? Did you change them (write them via the APIs) and nothing did happen?
Please note that if you step through your code with the VS2005 debugger you will change the App.vshost.exe.config file instead of you App.exe.config.

Yours,
Alois Kraus

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/4/2007 6:57 PM vikram
hi
your article has been very informative to me as im still a student.
but i faced a problem with your code,the changes i made from the form are not reflected in the config file of my application,however when i restarts my application they are still shown. im not getting where they are getting stored .
need not to say your help will be highly appriciated
thank you

# re: Read/Write App.config with .NET 2.0/Enterprise Library 5/7/2007 5:05 AM Inbaraj
Sir,
The code which u given is working.....??????? Did u checked it... and plz dont copy other site code and up it.. If ur doing so.. Better run once and put it...

reg
inbaraj

# re: Read/Write App.config with .NET 2.0/Enterprise Library 6/13/2007 6:05 AM takeo kokubo
I try folows yor exemple to wirte app.config file but your do not that. The app.config.file mantened original information without modification.
I use de same example in our article.
what happend
tnhanks for help me
takeo

# re: Read/Write App.config with .NET 2.0/Enterprise Library 7/2/2007 10:24 AM Alois Kraus
Hi Takeo,

if you try this inside the debugger you will end up modfying the yourApp.exe.vshost config file. You need to uncheck in the Projects - Debugging - "Enable the Visual Studio hosting process" checkbox to get the expected results.

Yours,
Alois


# re: Read/Write App.config with .NET 2.0/Enterprise Library 8/6/2007 10:53 PM Koka
value = set.Value as SettingValueElement;

Looks like this cast is needless
))

Other from this - great!!! 5 stars!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 9/12/2007 7:41 PM rawatjeet
Thanks for the nice article.

I have the following tag in the XML file and I want to read and check whether this exists. If exists then modify the NewVersion and if not then add the new assemblyBinding.

&lt;runtime&gt;
&lt;assemblyBinding&gt;
&lt;dependentAssembly&gt;
&lt;assemblyIdentity name="ABC.XYZ" publicKeyToken="cc0fa77b3e483175" /&gt;
&lt;bindingRedirect oldVersion="1.0.0.0-65535.65535.65535.65535" newVersion="7.6.1.0" /&gt;
&lt;/dependentAssembly&gt;
&lt;/assemblyBinding&gt;
&lt;/runtime&gt;

Any help will be appriciated.

Jeet

# re: Read/Write App.config with .NET 2.0/Enterprise Library 9/19/2007 5:40 AM Mansoor
Hello, excellent info.
however my feeble brain can not figure out how will i be able to modify an 'applicationSettings' element ? i can not even save appSettings elements ?

my application is a windows form, offering users a dialog box where they can give their a webservice url , and i want to save that url to the webservice nodes in 'applicationSettings' and some other settings in the appSettings, this is more like a stand alone application one user one application instance.

i will be very thankful for any help.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 9/20/2007 9:28 AM Mayra Orellana
My problem is: I have several projects within my solution. Each project has an AppConfig file, I need integrate the configuration values of all AppConfig files in an main AppConfig, How can I integrate all configuration values in the AppConfig file from a project that is the start up project? Thanks.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 10/15/2007 6:41 PM Tony
Hi there

Great article thanks, I found a similar solution in MSDN but my question refers to when you add your new appSettings item ... where is it written to? I receive the values when running the application but do not see any physical changes to my App.Config file in the project. I have disabled the VS hosting as well.

thanks

# re: Read/Write App.config with .NET 2.0/Enterprise Library 10/17/2007 12:56 AM Graham
Hi,
I've got the following code.
It populates the textbox OK, but doesn't update the config file with a new value.
What am I missing ?
thanks,
Graham

txtAuthorisedUsers.Text = ConfigurationManager.AppSettings["AdditionalAuthorisedUsers"].ToString();



System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("AdditionalAuthorisedUsers");
config.AppSettings.Settings.Add("AdditionalAuthorisedUsers",txtAuthorisedUsers.Text);
ConfigurationManager.RefreshSection("appSettings");


# Saving space in your comments section 11/16/2007 1:17 AM Neil Burnett
You might have replied to John (above and repeated below) privately. Could you send me the same reply? I am frustated that I don't know how to control the database name (and server instance) the entlib uses at runtime as the connectionString section in web.config is apparently hardcoded into the file.

---

Hi
If I don't know the Database name (for example) until run-time (when the User choses it from a List), how can I ensure that all the various Enterprise Libs, Membership providers get to use the new, correct ConnectionString?

Of course, I can write back to the app.config. But in my case the App runs on Terminal Services, and so I would have a problem with multiple users.

What I would like to do is have a Configuration String Provider, which all Libs use. But it does not seem that System.Configuration.Providers is intended for ths. Am trying to use a Protected Config String provider as all Libs will use the Decrypt method.
Do you have any other suggestions?
Thanks
John

# How can I update the memory cache with application's data configuration? 12/7/2007 5:10 AM Mayri
My problem is the following: I have many projects in a solution, each project has a file AppConfig . I need to load all configuration data of the all project's solution on memory cache. How can i do this? Can you help me please?





# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/29/2007 11:10 PM Antonio Zugaldia
Very useful indeed, many thanks!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 2/4/2008 11:14 PM Tatyana
Thanks a lot! very informative.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 3/5/2008 7:44 PM Timur
The way which is mentioned in
"Is this new mechanism compatible with the old one?" seems to be not compatible when we are talking about WinForms. Although application has settings, section "appSettings" contains zero items.
There is "applicationSettings" section in app.config, but you can't get it via ConfigurationManager. No luck.
And the real problem with that is that you can't update application-scoped settings with
System.Configuration.ApplicationSettingsBase.Save method, so it seems to me - no solution available for WinForms to update application-scoped settings on the fly.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 3/6/2008 4:40 AM admcse2001
Your article rocks!
Great info in manipulating app.config
Thanx.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 3/17/2008 6:33 AM Narasimha Reddy
Hi,
I developed a Windows application. I kept the connection string in the app.config file. The application is working fine in the develop invironment. I created the setup for this application added the app.config file to that and installed the applicication in my machine it self. When i try running the applciation it's throwing an error "The connection Sting property has not been initialized". It is working fine if i hot code the connection string.
Please tell me why am getting the error.
Thanks in advance

Narasimha Reddy Oggu.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/23/2008 12:33 AM Nitin Singhal
Hi,

I have a problem like i have a group of projects in which few of them are websites and few of them are windows dll apps. so my running apps is a website. Now i requred that in one of the windows dll project some configuration so i add a app.config file in it now when i read those setting i show me error. please help me.

Thanks in advance,

Nitin Singhal

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/23/2008 7:46 AM Hansi
Thank you very much - your article really helped me!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/24/2008 12:22 AM Maca
"... seem to seek guidance to the following questions:
* How to read/write to App.Config?"

This is exactly I was searching for. Thanks for providing an answer and sharing your thoughts. Have a great day mate!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 5/15/2008 7:06 AM Steve
Excellent article....Has answers to lot of questions..thanks!!1

# re: Read/Write App.config with .NET 2.0/Enterprise Library 5/29/2008 2:49 AM Andy
First: Excellent Post !!!

Now, my personal experience:
Imports System.Configuration won't work.
You won't be able to get ConfigurationManager that way.

You will need to (manually) add a reference to the Project for System.Configuration


# re: Read/Write App.config with .NET 2.0/Enterprise Library 5/30/2008 10:39 AM manjula
Hi...i'm net to .NET..can u pls exaplain me the difference between app.exe.config and app.config

# re: Read/Write App.config with .NET 2.0/Enterprise Library 8/7/2008 10:05 AM frustrated
@Andy:

YOU NEED A REFERENCE TO System.Configuration, OF COURSE !!!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 11/5/2008 1:19 AM George
Hi Alois Kraus,


I read the document you wrote carefully. Really informative and technical description in-depth. I have 3 problems after reading is,

1.

In section Using the MySettings Feature, it is mentioned "... create an access class ..." is the access class means class Settings?

2.

In section Using the MySettings Feature, it is mentioned "... When you add a settings file to your class library project you can merge the settings with the App.config file of your hosting executable ...", I am wondering how to merge the application configuration setting for a class library into the hosting executable? Manually copy settings from class library to hosting executable settings or there are some automatic ways?

3.

I like the GUI to set configuration as showed in the figure in section Using the MySettings Feature, and seems we can use this GUI in console/Form/class library projects. My understanding is such GUI will generate two outputs, the actual content in app.config file and an easy to use wrapper class, correct?

But I do not think the wrapper class provides too much benefits since even if not using the wrapper class, using Configuration API to access app.config is not that hard. Any coments?


regards,
George

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/18/2008 3:51 PM Jim
Very useful and concise. Thanks for your help.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/23/2008 11:52 AM Alois Kraus
Hi Kumar,

to change the connection strings you do not need to go through the Properties or something similar since the generated classes by the settings designer are not related to the connection strings section at all.
To change the connection string you can try to following code:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"ConsoleApplication2.exe.Config";
// Open another config file
Configuration _config1 = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

ConnectionStringsSection mySection = (ConnectionStringsSection) _config1.GetSection("connectionStrings");
_config1.ConnectionStrings.ConnectionStrings["ConsoleApplication2.Settings1.Setting"].ConnectionString = "some ohter string";
_config1.Save(ConfigurationSaveMode.Full);

Yours,
Alois Kraus


# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/23/2008 11:38 PM Kiron Nandakumar
Hello Alois Kraus,

Thanks for you immediate reply. Still im having some issues with the connection string supplied to access DAL at runtime. I have got a setting which has one connection string attached. whatever you have given in your reply is fine. That works for my main projects app.config only. My question is how to configure the connection string for some other class library as well. My objective is to recieve a connection string runtime, which im able to accomplish, and all the connection should refer to this one.
One more thing is that the scope of my setting is set to Application, not user. I couldn't change that as well.
Expecting your reply.

Thanks n Regards,

Kiron Nandakumar

# re: Read/Write App.config with .NET 2.0/Enterprise Library 1/14/2009 8:25 AM Evgeniy Podolyak
Great Article! Thanks!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 3/24/2009 2:05 AM Michael Hidalgo Fallas
Hi all.
I need to access from a class library , the appconfig located on the Main Application, because of each applications has it own configuration.

How can I do this ? Thanks

# re: Read/Write App.config with .NET 2.0/Enterprise Library 3/24/2009 2:52 AM Alois Kraus
That should do the trick:

static void LoadAppCfgAndPrintAppSettings()
{
// Load the executable App.config file. It may be differnt for your AppDomain if you have more
// than one !! In that case you have to use string appcfg = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(Process.GetCurrentProcess().MainModule.FileName);

foreach (string key in config.AppSettings.Settings.AllKeys)
{
Console.WriteLine("Key: {0}, Value: {1}", key, config.AppSettings.Settings[key].Value);
}
}

Yours,
Alois Kraus


# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/23/2009 9:02 PM Amos Sellophane
Hi, This is a great write up - thank you very much.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/29/2009 8:56 PM Fadl
Greate resource 10x a lot

# re: Read/Write App.config with .NET 2.0/Enterprise Library 4/30/2009 9:01 PM rudi
Hi,

I want to configure a program I wrote during instalation. So I added an Installer class to my program that takes some parameters from the user during setup and writes to the app.config. The writing to the app.config part doesn't work.
Is is posible to write to the App.config file during the itstallation of the program (during the execution of the setup - .msi file)?

I succeded in writing to the app.config from my program (not during setup) but after the config.save() the new values are saved but all the comments I had in my app.config were removed and the order of the keys was changed... Is there a way to save the new keys without removal of the comments and reordering of keys?

Thanks!

# re: Read/Write App.config with .NET 2.0/Enterprise Library 7/27/2009 4:12 AM Amitha
Hello,
I have a product which has already gone into production.It is using a customconfig with abc.config.It has both read/write into it.Even the usersettings are written into the same using xml parsing and creating element on the fly.The same is read and written back during the application flow.All the UI feature is persisted into it thru xml read/write.Now for the next version,they have asked me to use AppsettingsBase to just move the usersettings into another file so that the appsettings and usersettings are kept apart in 2 diff files.I have implemented the usersettings for the project.I am not able to find a way of migrating the old config persisted details to my new config file.I need to do this as my product has already gone on production.So user wud have some values persisted in his old config file.How will i read that from old file and write back to my new file.
Please help.Any ideas are welcome.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 10/13/2009 4:20 PM vitor rubio
Good Article.
First, sorry my bad english :P

I'm tryingo to put into configuration --> appsettings, in my app.config file a key "MailTo" with value "Some Alias<mail@domain.com>", like this:

<add key="MailTo" value="Vitor Rubio<vitor@rt.com.br>"></add>

But I can't do that, because of the brackets "<>".

How can I do this?

Thanks

# re: Read/Write App.config with .NET 2.0/Enterprise Library 10/13/2009 5:52 PM Alex Iurovetski
@vitor rubio: You should either use &lt; and &rt; or CDATA section (which is an overkill in my opinion)

# re: Read/Write App.config with .NET 2.0/Enterprise Library 10/13/2009 5:53 PM Alex Iurovetski
@vitor rubio: Sorry, &lt; and &gt;

# re: Read/Write App.config with .NET 2.0/Enterprise Library 10/20/2009 5:58 AM @yehuda
Alois,
Useful post.

I'm in the process of using your example. Am finding it saves the new setting to the appName.vshost.exe.config (as indicated by windows explorer) but as soon as I stop debugging the program in VS it REVERTS the file back to the way it was before hand.
Is there a setting somewhere that is causing this? Is there any way to stop this?

# Problem when loading the XML configuration file with connection string from COM 11/9/2009 9:39 PM lilliy
Hi All,

I created a .Net application which reads from the SQL filestream database.

I created a app.config file

<appSettings>

<add key="sqlConnectionString" value="server=Mattrus-3B811A0\SQLEXPRESS;database=FILESTREAMExam;Integrated Security=True"/>

</appSettings>

when i try loading this connection string from .Net application it works fine,

when I created a type library of this .Net application , a dll for this XML configuration file is also generated.

I tried to load it from COM i load the type library as

#import "..\<Name of type library>.tlb" raw_interfaces_only

but how do i load the XML app.config file in COM so that the type library picks ups the connection string from the configuration file

Can anyone help me out with this


# re: Read/Write App.config with .NET 3.0/Enterprise Library 8/8/2010 5:47 PM SrikanthReddy
Hi,

I am using same concept as explained above in .Net 2008. But App.config file is not updating.
Code:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//config.AppSettings.Settings.Remove("Separator");
config.AppSettings.Settings.Add("Separators", "*");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

App.Config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Separator" value=";"/>
</appSettings>
</configuration>

can anyone help this issue.

# Modify application.dll.config after combine my application 9/6/2010 10:03 PM LanNguyen
My solution have 3 project, each project have 1 app.config file. I create a main project, and add reference to 3 project. In main project have 1 app.config file.
Then, when i combine my Application, i always copy debug folder to other place and use .exe file to run my application.
Problem is: change 3 file config of 3 project, don't have to combine again. Does my application run with a new setting?
How can i do that?
Please show me how, i am highly appreciate your help.
Thanks

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/27/2010 1:39 AM jean-pierre a
I'll thank you if you could show me howto move the "blabla.exe.config" or whatever in-an-user-writable-place !!!

As you know, in the recent versions of windows, as in Unix, the executables are sited in write protected places.
For example, as an W7 Administrator, try to write somewhere in a ProgramFiles place. You'll be surprised... And it's very well as is : Bravo Krosoft, en fin vous voilà intelligents

So, user's program preferences had to be written in user-local-permitted-places, not in executable's folder

UNFORTUNATELY ALL THIS PAPER HAS TO BE REWRITTEN.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 12/27/2010 10:11 PM Alois Kraus
The default configuration manager of .Net does read the App.config from the location where the executable is located. You are right you are not allowed to write to this location but thanks to Windows 7 the OS does not deny it any longer but redirect writes to Programm Files folders to the local users profile which I think is way better than a simple access denied.
I have shown plenty of ways how you can read/write your settings from other places e.g.

Configuration config = ConfigurationManager.OpenExeConfiguration(@"Path to your file");
config.Sections.Remove("EditorSettings");
config.Sections.Add("EditorSettings", configData);
config.Save();

You only need to use a different overload to load app.configs from arbitrary places.

Or you can use the supplied designer for Windows Forms applications where you can select the policy where the settings should stay.

You can install your application into the user profile than all is fine (I do think Click Once does that) and you have full freedom.
If you want to install into Programm Files you can do this of course and permit writes beneath your application to e.g. only authenticated users.

All solutions do work. Its up to you to choose from the options you have the most appropriate for your specific problem.

Yours,
Alois Kraus


# re: Read/Write App.config with .NET 2.0/Enterprise Library 1/5/2011 4:47 AM Andy F
This is a great write-up and I thank you very much for it - its very helpful.

However, I still think (as many of my collegues do) that this is the usual total overkill on the part of Microsoft. I often wonder if these "intelligent MS developers" who dont have one scintilla of common sense actually bend over backwards to tie their shoes, just to make it more complex.

I understand all the security and that stuff, but take our company, we write apps that are NEVER user-based configured, they are app-based configured and it would have been very easy for MS to provide for that. Instead, we spent months trying to work through this stuff that MS calls "simple", when in fact, its overly-complex. Indeed, we did two support calls with their techs and even they could not answer some questions about it!

I praise you for your article. Its the first I have read that at least helps us get through all this stuff.

Of course, now we have a ton of re-writing to do.

One final thought, and we shared this with MS... They kept saying "security requires..." this and that. Really? Well we use .NET encryption on most of our config settings so if we are encrypting them, why the heck do they have to be stored every which way to Sunday??? Its like a bank that has a safe with a good combination, then hiding the safe itself so no one can even find the money - and that includes (!) the people who need to get at it!

None the less, again, thanks! Youre a life-saver!







# re: Read/Write App.config with .NET 2.0/Enterprise Library 2/23/2011 11:27 PM Rahul Rawade
This is a great write-up and I thank you very much for it - its very helpful.


# re: Read/Write App.config with .NET 2.0/Enterprise Library 9/24/2011 12:30 AM p9o
However, I still think (as many of my collegues do) that this is the usual total overkill on the part of Microsoft. I often wonder if these "intelligent MS developers" who dont have one scintilla of common sense actually bend over backwards to tie their shoes, just to make it more complex.As you know, in the recent versions of windows, as in Unix, the executables are sited in write protected places.
For example, as an W7 Administrator, try to write somewhere in a ProgramFiles place. You'll be surprised... And it's very well as is : Bravo Krosoft, en fin vous voilà intelligents

# re: Read/Write App.config with .NET 2.0/Enterprise Library 9/24/2011 8:01 AM Alois Kraus
Partially I do agree that many of the features of the System.Configuration namespace are not needed for most scenarios. But there are scenarios where these things are highly useful (e.g. replace the mechanism to write to a datbase instead of a file).
As you have found out you can write to the Program Files folder because many developers did not care for security a very long time. MS does care and redirects writes to the user profile of the current user. If the developer does care about security and provide a manifest that marks the application as UAC aware you will get access denied as you should have always. But this would have broken way to many apps. Security is a complex topic. You can read the full details here: http://windowsteamblog.com/windows/b/developers/archive/2009/08/04/user-account-control-data-redirection.aspx

Yours,
Alois Kraus



# Mr 11/10/2011 4:15 AM Bharat Mistry
Thanks for the solution it has helped a lot.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 5/28/2012 1:25 PM JuanFra
Muy buen articulo, me ayudó lo suficiente para resolver un problema que traía...gracias y saludos.

# re: Read/Write App.config with .NET 2.0/Enterprise Library 3/4/2014 11:14 PM priya
too good,Very clear.Saved my day..Thank you

Post A Comment
Title:
Name:
Email:
Comment:
Verification: