I've been trying to save a hashtable in an Application Settings file.
I need to save settings for each tabPage on a form. Trouble is, the
number of tabPages is determined at runtime, so I can't have individual
settings for each page. I decided to hold them in a collection.
I started out with a generic Dictionary but that didn't work. So, I
created a new Forms app to test it out and created a Settings file. It
didn't offer me the generic Dictionary but did off the old Hashtable so
I tried that. It doesn't seem to save any values for the Hashtable:
Chair saves but table doesn't. This is my code: (the user.config that
results is at the end)
Any idea why Hashtable doesn't save anything. Any idea of a way round
this?
In form1's constructor (after InitializeComponent();):
Settings _settings = new Settings();
_settings.Chair = "Four-legged";
_settings.Table = new System.Collections.Hashtable();
_settings.Table.Add("First",
new TreeViewListViewSettings(View.Details));
_settings.Save();
Add a new object of type settings called Settings and use the Designer
to add these two items:
Name Type Scope Value
Table System.Collections.Hashtable User
Chair string User
A class to hold the values (I've only put one it is for clarity):
==========================
[Serializable()]
public class TreeViewListViewSettings
{
public TreeViewListViewSettings()
{
}
public TreeViewListViewSettings(View startingView)
{
_startingView = startingView;
}
public View StartingView
{
get { return _startingView; }
set { _startingView = value; }
}
private View _startingView;
}
user.config
===========
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ApplicationSettings.Settings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<ApplicationSettings.Settings>
<setting name="Chair" serializeAs="String">
<value>Four-legged</value>
</setting>
<setting name="Table" serializeAs="Xml">
<value />
</setting>
</ApplicationSettings.Settings>
</userSettings>
</configuration>