-
Jul 24th, 2020, 05:44 PM
#1
Thread Starter
Hyperactive Member
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."
-
Jul 25th, 2020, 01:56 AM
#2
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
-
Jul 27th, 2020, 10:04 AM
#3
Thread Starter
Hyperactive Member
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."
-
Aug 22nd, 2020, 07:50 PM
#4
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
-
Aug 24th, 2020, 08:27 AM
#5
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|