Results 1 to 5 of 5

Thread: Creating user settings on the fly and issue with debugging an app using user settings

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2004
    Location
    Kansas, USA
    Posts
    352

    Creating user settings on the fly and issue with debugging an app using user settings

    I need to be able to store user settings so that they can be retrieved the next time the app is run. Basically the settings I need to save are various paths for file opening and saving. Part of my issue is when I use Settings.Default.Properties.Add it seems to be adding user settings but they not show up next time I debug.

    I also want to create a new setting "on the fly" as needed since I don't want to add these settings in the settings table in "Properties -> Settings" and recompile.

    My code looks like this:

    Code:
       public class Config
        {
            public SettingsProperty GetSetting(string name)
            {
                var enumerator = Settings.Default.Properties.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    SettingsProperty setting = (SettingsProperty)enumerator.Current;
                    if (setting.Name == name)
                    {
                        return setting;
                    }
                }
                return null;
            }
    
            public void SetSetting(SettingsProperty property)
            {
                var enumerator = Settings.Default.Properties.GetEnumerator();
                bool found = false;
                while (enumerator.MoveNext())
                {
                    SettingsProperty setting = (SettingsProperty)enumerator.Current;
                    if (setting.Name == property.Name)
                    {
                        setting = property;
                        Settings.Default.Save();
                        found = true;
                    }
                }
                if (!found)
                {
                    Settings.Default.Properties.Add(property);
                    Settings.Default.Save();
                }
            }
        }
    If I create a new setting using that code It seems to save OK. I can see the new setting in properties in debug but they disappear when I close the app. What am I doing wrong?
    Thanks,
    Eric
    --------------------------------------------------------------------------------------------------------------------
    VB.net/C# ... Visual Studio 2019
    "None of us are as smart as all of us."

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Creating user settings on the fly and issue with debugging an app using user sett

    What you're asking for is not supported. I haven't read everything, let alone tested it, but you might find something useful here:

    https://stackoverflow.com/questions/...gs-at-run-time

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2004
    Location
    Kansas, USA
    Posts
    352

    Re: Creating user settings on the fly and issue with debugging an app using user sett

    Thanks @jmcihinney. That make sense as to why creating new settings on the fly didn't work.
    Thanks,
    Eric
    --------------------------------------------------------------------------------------------------------------------
    VB.net/C# ... Visual Studio 2019
    "None of us are as smart as all of us."

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Creating user settings on the fly and issue with debugging an app using user sett

    You could use the ConfigurationManager to save settings to a per-user app.config file, ex:

    Code:
                // Open and editable configuration file for use by the current user            
                var roamingConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
                var map = new ExeConfigurationFileMap { ExeConfigFilename = roamingConfig.FilePath };
                var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                
                // Get the setting (if it's present)
                var setting = config.AppSettings.Settings["MySetting"]?.Value;
                
                Console.WriteLine($"Setting is: {setting}");
                
                // Save the setting for next time
                config.AppSettings.Settings.Remove("MySetting");
                config.AppSettings.Settings.Add("MySetting", "MyValue");
                config.Save(ConfigurationSaveMode.Minimal, false);
    Regards,

    - Aaron

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2004
    Location
    Kansas, USA
    Posts
    352

    Re: Creating user settings on the fly and issue with debugging an app using user sett

    Thanks @Aaron Young. I like that even better.
    Thanks,
    Eric
    --------------------------------------------------------------------------------------------------------------------
    VB.net/C# ... Visual Studio 2019
    "None of us are as smart as all of us."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width