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.