6 Comments

  1. Samanthasays:

    This works great! But I do have a question for you (I am new to C# and app development)…

    Why, when I make this component a separate function, does it no longer work correctly?

    YOUR CODE:
    public void setAppSetting(string key, string value)
    {
    //Load AppSettingss
    Configuration config = ConfigurationManager.
    OpenExeConfiguration(
    System.Reflection.Assembly.
    GetExecutingAssembly().Location);
    //Check if entry (which matches the key) exists
    if (config.AppSettings.Settings[key] != null)
    {
    //Delete it for “overwrite” process
    config.AppSettings.Settings.Remove(key);
    }
    //Create new KeyValue pair
    config.AppSettings.Settings.Add(key, value);
    //Save the changed settings
    config.Save(ConfigurationSaveMode.Modified);
    }

    MY NEW CODE:
    public void setAppSetting(string key, string value)
    {
    //Load AppSettingss
    Configuration config = ConfigurationManager.
    OpenExeConfiguration(
    System.Reflection.Assembly.
    GetExecutingAssembly().Location);

    deleteAppSettings(key);

    //Create new KeyValue pair
    config.AppSettings.Settings.Add(key, value);
    //Save the changed settings
    config.Save(ConfigurationSaveMode.Modified);
    }

    public delete deleteAppSettings(string key)
    {
    //Check if entry (which matches the key) exists
    if (config.AppSettings.Settings[key] != null)
    {
    //Delete it for “overwrite” process
    config.AppSettings.Settings.Remove(key);
    }
    }

    • Samanthasays:

      Sorry I ment
      public void deleteAppSettings(string key)

      not
      public delete deleteAppSettings(string key)

      • Samanthasays:

        public void deleteAppSetting(string key)
        {

        //Load AppSettings
        Configuration config = ConfigurationManager.
        OpenExeConfiguration(
        System.Reflection.Assembly.
        GetExecutingAssembly().Location);

        //Delete key
        if (config.AppSettings.Settings[key] != null)
        { config.AppSettings.Settings.Remove(key); }
        }

        • I don’t see, why your code shouldn’t run. What error code do you get?

          • Samanthasays:

            It displays all the past values for each field on the UI as a continuous string. I am not sure why it would do that though… the only difference is it is in a different module…

  2. Kajteksays:

    I haven’t tested it, but you presented it clearly and I understand what you are doing. Thank you for sharing.

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked