Write values in app.config file

后端 未结 10 923
生来不讨喜
生来不讨喜 2020-11-29 01:46

can anyone please help me how can I set/store values in the app.config file using c#, is it possible at all?

相关标签:
10条回答
  • 2020-11-29 02:08

    For a .NET 4.0 console application, none of these worked for me. So I used the following below and it worked:

    private static void UpdateSetting(string key, string value)
    {
        Configuration configuration = ConfigurationManager.
            OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
        configuration.AppSettings.Settings[key].Value = value;
        configuration.Save();
    
        ConfigurationManager.RefreshSection("appSettings");
    }
    
    0 讨论(0)
  • 2020-11-29 02:09
    string filePath = System.IO.Path.GetFullPath("settings.app.config");
    
    var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
    try
    {
        // Open App.Config of executable
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    
        // Add an Application Setting if not exist
    
            config.AppSettings.Settings.Add("key1", "value1");
            config.AppSettings.Settings.Add("key2", "value2");
    
        // Save the changes in App.config file.
        config.Save(ConfigurationSaveMode.Modified);
    
        // Force a reload of a changed section.
        ConfigurationManager.RefreshSection("appSettings");
    }
    catch (ConfigurationErrorsException ex)
    {
        if (ex.BareMessage == "Root element is missing.")
        {
            File.Delete(filePath);
            return;
        }
        MessageBox.Show(ex.Message);
    }
    
    0 讨论(0)
  • 2020-11-29 02:12
    private static string GetSetting(string key)
    {
        return ConfigurationManager.AppSettings[key];
    }
    
    private static void SetSetting(string key, string value)
    {
        Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        configuration.AppSettings.Settings[key].Value = value;
        configuration.Save(ConfigurationSaveMode.Full, true);
        ConfigurationManager.RefreshSection("appSettings");
    }
    
    0 讨论(0)
  • 2020-11-29 02:12

    As others mentioned, you can do this with ConfigurationManager.AppSettings.Settings. But: Using Settings[key] = value will not work if the key doesn't exist.
    Using Settings.Add(key, value), if the key already exists, it will join the new value to its value(s) separated by a comma, something like <add key="myKey" value="value1, value2, value3" />

    To avoid these unexpected results, you have to handle two scenario's

    • If entry with the given key exists? then update its value
    • if entry with the given key doesn't exist? then create new entry(key,value)

    Code

    public static void Set(string key, string value)
    {
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
        var entry = config.AppSettings.Settings[key];
        if (entry == null)
            config.AppSettings.Settings.Add(key, value);
        else
            config.AppSettings.Settings[key].Value = value;
    
        config.Save(ConfigurationSaveMode.Modified);
    }
    

    For more info about the check entry == null, check this post.
    Hope this will help someone.

    0 讨论(0)
提交回复
热议问题