Joe
Joe

Reputation: 35

Modify appSettings in runtime without writing to disk

This is a desktop application created with .NET Framework in Windows Forms.

I want to create a way to overwrite app.config->appSettings values.

When the project init it calls a function that get params from a database matching appName.

This is the function:

    private void LoadAppConfigs()
    {
        var appConfigs = Connection.GetAppConfigs();
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        foreach (var c in appConfigs)
        {
            config.AppSettings.Settings[c.Key].Value = c.Value;
        }

        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
    }

It works, but this implies writing to disk (modifying app.config) which is problematic because users doesn't have access to write to the disk.

Is there a way to modify AppSettings at runtime without writing to disk?

Is there another way I can use to achieve this?

Upvotes: 2

Views: 748

Answers (1)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

Based on my research, I find two ways to modify the appsettings at runtime.

First, you can use XmlDocument to change it. As follows:

            XmlDocument xml = new XmlDocument();
            string basePath = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName;
            string path = basePath + @"\App.config";
            xml.Load(path);
            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;            
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            xNode = xml.SelectSingleNode("//appSettings");
            string AppKey = "Setting2";
            string AppValue = "Expensive";
            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null)
            {
                xElem1.SetAttribute("value", AppValue);
                cfa.AppSettings.Settings[AppKey].Value = AppValue;
            }
            else
            {
                xElem2 = xml.CreateElement("add");
                xElem2.SetAttribute("key", AppKey);
                xElem2.SetAttribute("value", AppValue);
                xNode.AppendChild(xElem2);
                cfa.AppSettings.Settings.Add(AppKey, AppValue);
            }
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            xml.Save(path);

Second, you can also use config.AppSettings.Settings[key].Value =value;. Code:

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    config.AppSettings.Settings["Setting2"].Value = "Expensive";
    config.Save();//exe.config change
    string basePath = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName;
    string path = basePath + @"\App.config";
    config.SaveAs(path);//App.config change
    ConfigurationManager.RefreshSection("appSettings");

Last but not least, you can refer to How do I force my .NET application to run as administrator? to know how to solve user permission problem.

Upvotes: 1

Related Questions