Properties.Settings NameValueCollection doesn't work?

G

Guest

For some unknown reason (user error?), I cannot get a NameValueCollection to
persist in the app.config file.

Unlike other settings, I cannot get the String Collection Editor GUI to
allow my to add/edit any values for a setting with type NameValueCollection.

Nor can I get a NameValueCollection to persist to the User Settings via code
using a simple C# Console App...

Is this a user error or ?

Thanks,
Rick


Here's the code behind:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;

namespace Play_With_Settings
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting...");

if (Properties.Settings.Default.MyNameValueCollection == null)
{
Properties.Settings.Default.MyNameValueCollection = new
NameValueCollection();
}

Console.WriteLine(Properties.Settings.Default.MyUserSetting);

Console.WriteLine(Properties.Settings.Default.MyNameValueCollection.Count);

Properties.Settings.Default.MyUserSetting = "Hello Saturn!";

Properties.Settings.Default.MyNameValueCollection.Add("name1",
"value1");
Properties.Settings.Default.MyNameValueCollection.Add("name2",
"value2");
Properties.Settings.Default.MyNameValueCollection.Add("name3",
"value3");

Properties.Settings.Default.Save();


Console.WriteLine(Properties.Settings.Default.MyNameValueCollection.Count);
Console.WriteLine(Properties.Settings.Default.MyUserSetting);

}
}
}

And now the app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Play_With_Settings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Play_With_Settings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Play_With_Settings.Properties.Settings>
<setting name="MyValue" serializeAs="String">
<value>Hello World</value>
</setting>
</Play_With_Settings.Properties.Settings>
</applicationSettings>
<userSettings>
<Play_With_Settings.Properties.Settings>
<setting name="MyUserSetting" serializeAs="String">
<value>Hello Mars</value>
</setting>
<setting name="MyTest" serializeAs="Xml">
<value>
<ArrayOfString
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>x=y</string>
<string>1=2</string>
<string>a=b</string>
</ArrayOfString>
</value>
</setting>
</Play_With_Settings.Properties.Settings>
</userSettings>
</configuration>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top