Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# applicationSettings: how to update app.config?

Tags:

c#

app-config

I am using .NET 4.0 and I would like to use the app.config file to store same parameter settings. I do the following. I use the Settings tab in the project properties to create my parameters.

This add the information in the app.config file like this:

<MyApp.Properties.Settings>
      <setting name="param1" serializeAs="String">
        <value>True</value>
      </setting>
<MyApp.Properties.Settings>

In my view model (in my code) I can access to the information in this way:

bool myBool = MyApp.Properties.Default.param1;

When I try to change the value in the config file, I try this:

Properties.Settings.Default.param1 = false;

But this causes an error, that param1 is read-only.

So how can I update my config file from my code?

like image 239
Álvaro García Avatar asked Nov 08 '12 10:11

Álvaro García


2 Answers

Here's my function to update or add an entry into the app.config for "applicationSettings" section. There might be a better way, but this works for me. If anyone can suggest a better method please share it, we'll always looking for something better.

    static string APPNODE = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Properties.Settings";
    static DateTime now = DateTime.Now;
    Utilities.UpdateConfig(APPNODE, "lastQueryTime", now.ToString());

    static public void UpdateConfig(string section, string key, string value)
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        ClientSettingsSection applicationSettingsSection = (ClientSettingsSection)config.SectionGroups["applicationSettings"].Sections[section];
        SettingElement element = applicationSettingsSection.Settings.Get(key);

        if (null != element)
        {
            applicationSettingsSection.Settings.Remove(element);
            element.Value.ValueXml.InnerXml = value;
            applicationSettingsSection.Settings.Add(element);
        }
        else
        {
            element = new SettingElement(key, SettingsSerializeAs.String);
            element.Value = new SettingValueElement();
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            element.Value.ValueXml = doc.CreateElement("value");

            element.Value.ValueXml.InnerXml = value;
            applicationSettingsSection.Settings.Add(element);
        }

        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("applicationSettings");            
    }
like image 131
Manny Avatar answered Oct 15 '22 14:10

Manny


Well, I read the link of Hari Gillala, in which one user suggested to edit directly the app.config file, that is a xml file.

So in project properties-->settings I create the parameters that I need. Then, to load a parameter in code I do the following:

_myViewModelProperty = MyApp.Properties.Settings.Default.MyParam1;

In this way, I can read easily the information of the config parameter. Is typed, so in disign time I can see if the asign is correct or not.

To update de config file, I edit the app.config file with the xml libraries of .NET.

    System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
    xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    System.Xml.XmlNode node;
    node = xml.SelectSingleNode("configuration/applicationSettings/MyApp.Properties.Settings/setting[@name='myparam1']");
    node.ChildNodes[0].InnerText = myNewValue;
    xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

In this way, I create a xml document (xml variable) and load the information of the app.config file. Then, I search for the node that I want to update, update its information (InnerText property) and finally I save the changes.

In this way, I can update the app.config. It is what I want, because in a portable application, only one user will use it, and I want that the configuration is applied in any computer in which I run the application.

like image 30
Álvaro García Avatar answered Oct 15 '22 15:10

Álvaro García