0

I'm trying to create a custom settings class to store some user-scoped settings. I did this few months ago and it worked just fine, but I can't remember all the details and don't have access to the source code anymore. Here's what I have so far:

CustomSettings.cs

internal sealed partial class CustomSettings : global::System.Configuration.ApplicationSettingsBase
{
    private static CustomSettings defaultInstance = ((CustomSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new CustomSettings())));

    public static CustomSettings Default
    {
        get
        {
            return defaultInstance;
        }
    }

    [global::System.Configuration.UserScopedSettingAttribute()]
    public global::System.Collections.Generic.List<global::LearningProject.Types.Product> Products
    {
        get
        {
            return ((global::System.Collections.Generic.List<global::LearningProject.Types.Product>)(this["Products"]));
        }
        set
        {
            this["Products"] = value;
        }
    }
}

Product.cs

[Serializable]
public class Product
{
    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlAttribute("Id")]
    public int Id { get; set; }
}

Here's the App.config file, and contains a sample Product:

<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="LearningProject.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
      <section name="LearningProject.CustomSettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
  <userSettings>
        <LearningProject.CustomSettings>
          <setting name="Products" serializeAs="Xml">
            <ArrayOfProduct>
              <Product>
                <Name>Product1</Name>
                <Id>ProductID</Id>
              </Product>
            </ArrayOfProduct>
          </setting>
        </LearningProject.CustomSettings>
    </userSettings>
</configuration>

Now I get null when I try to get a list of Products:

List<Product> products = CustomSettings.Default.Products;

Which means either I'm doing something wrong (xml attributes, maybe?) or I'm missing a step. Also notice that a Settings1.settings (generated by VS) already exists, and my CustomSettings class is just adding methods to the default auto-generated settings file. I was just wondering if anyone has ever done this. Any help is appreciated.

Arian Motamedi
  • 7,123
  • 10
  • 42
  • 82

1 Answers1

1

In app.config replace

<setting name="Products" serializeAs="Xml">
  <ArrayOfProduct>
    <Product>
      <Name>Product1</Name>
      <Id>ProductID</Id>
    </Product>
  </ArrayOfProduct>
</setting>

To

<setting name="Products" serializeAs="Xml">
  <value>
    <ArrayOfProduct>
      <Product Name="Product1" Id="1" />
    </ArrayOfProduct>
  </value>
</setting>

How I realized this

CustomSettings.Default.Products = new List<Product>();
CustomSettings.Default.Products.Add(new Product() { Id = 1, Name = "Product1" });
CustomSettings.Default.Save();

Now local config (%appdatalocal%\project_name..\user.config) contains the correct value

cdmnk
  • 314
  • 1
  • 11
  • I still get null when I add them to `app.config` manually, but adding it through code seems to work, but even then I still can't see them in the `app.config`, which suggests that they're getting saved somewhere else. Any idea? – Arian Motamedi Jun 26 '13 at 16:23
  • Corrected my answer: forgot about tag.Local config located in %appdatalocal%\project_name..\user.config. Delete it when you're done, because otherwise the settings will be taken from it – cdmnk Jun 27 '13 at 10:18
  • But how can I put the settings directly in app.config? When I first did this months ago, all the settings were written/read in the app.config. – Arian Motamedi Jun 27 '13 at 16:04
  • [link](http://msdn.microsoft.com/en-us/library/system.configuration.configurationelement.aspx) see code example. With following code in Main() it will work perfectly `Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); UrlsSection mySection = (UrlsSection)config.Sections["MyFavorites"];` – cdmnk Jun 28 '13 at 10:46
  • Ok that gives me an idea ;) – Arian Motamedi Jun 28 '13 at 16:07