0

I am trying to save user settings in C# and reuse them in the next session.

All over the net, one finds tipps like this: How to save user settings programatically?

I have a .setting file in my project with the default settings.

When a user changes a setting, I call save, and in %APPDATA% the file appears, with the user settings. It looks like this:

private void SetSetting(string prop, Object val)
{
    try
    {
        string strval = String.Format("G{0}_{1}", Group_Id, prop);
        Properties.Last.Default[strval] = val;
    }
    catch
    {
        System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty(String.Format("G{0}_{1}", Group_Id, prop));
        property.PropertyType = Properties.Def.Default[String.Format("G0_{1}", Group_Id, prop)].GetType();
        property.DefaultValue = Properties.Def.Default[String.Format("G0_{1}", Group_Id, prop)];
        property.PropertyType = val.GetType();
        property.Provider = Properties.Last.Default.Providers["LocalFileSettingsProvider"];
        property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
        Properties.Last.Default.Properties.Add(property);
        Properties.Last.Default.Reload();
        string strval = String.Format("G{0}_{1}", Group_Id, prop);
        Properties.Last.Default[strval] = val;
    }
    Properties.Last.Default.Save();
}

As you can see, I have the settings instance Last as in "settings from last session" and Def as in default.

Basically, a user can add and delete groups at will in the app. Def holds the default configuration for a new group. Last should hold the number of groups in the last user session, as well as the properties of each group. Thus the formatting with the Group_Id and so on.

So, as I said. During runtime, its fine. User settings are properly written to the file in %APPDATA% and everything.

The file is like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5629387e089" >
            <section name="Project1.Properties.Last" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5629387e089" " allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <Project1.Properties.Last>
            <setting name="G1_do_cut" serializeAs="String">
                <value>True</value>
            </setting>
        </Project1.Properties.Last>
    </userSettings>
</configuration>

So, the setting for do_cut for Group 1 is saved as G1_do_cut

But as soon as I restart the application, the user setting is ignored. The code used is:

private Object GetSetting(string prop)
{
    try { return Properties.Last.Default[String.Format("G{0}_{1}", Group_Id, prop)]; }
    catch { return Properties.Def.Default[String.Format("G0_{1}", Group_Id, prop)]; }
}

and in the try, an exception G1_do_cut is not found in settings is thrown and the catch loads the default.

During a user session, the GetSettings works correctly - if I try to read G1_do_cut after it has been set, no exception is thrown and I get the correct value.

How do I get C# to reuse the settings after restarting the application?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
user3696412
  • 1,321
  • 3
  • 15
  • 33

0 Answers0