By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Properties.Settings NameValueCollection doesn't work?

P: n/a
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.MyUs erSetting);

Console.WriteLine(Properties.Settings.Default.MyNa meValueCollection.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.MyNa meValueCollection.Count);
Console.WriteLine(Properties.Settings.Default.MyUs erSetting);

}
}
}

And now the app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGrou p, 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>
Dec 15 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.