Groups | Blog | Home
all groups > dotnet framework > may 2008 >

dotnet framework : Store Dictionary in Config File?


coconet
5/29/2008 1:06:58 PM
I have a list of value pairs represented as a
Dictionary<String,String>. I would like to store them in my app.config
file - how can I do that? I don't want to keep them in a regular text
file.

Thanks.
James
5/30/2008 9:19:41 AM
Most info I need between sessions I put into the registry. 120 sounds like a
lot, however.

UserSubKey I use as my appname.
KeyName and StringValue a key/value pair.

hkCurrentUser is set to the registry hive called CurrentUser.

Cut and Paste in these two pieces and test one of your country codes.

'Save an entry
Public Sub SaveSettings(ByVal UserSubKey As String, ByVal KeyName As
String, ByVal StringValue As String)
Dim hkCurrentUser As Microsoft.Win32.RegistryKey =
Microsoft.Win32.Registry.CurrentUser 'HKEY_Current_User
Dim regKey As Microsoft.Win32.RegistryKey

regKey = hkCurrentUser.CreateSubKey(UserSubKey)
regKey.SetValue(KeyName, StringValue)

End Sub


'Retreive an entry
Public Function GetSettings(ByVal UserSubKey As String, ByVal KeyName As
String) As String
Dim hkCurrentUser As Microsoft.Win32.RegistryKey =
Microsoft.Win32.Registry.CurrentUser 'HKEY_Current_User
Dim regKey As Microsoft.Win32.RegistryKey

regKey = hkCurrentUser.OpenSubKey(UserSubKey)
'HKEY_Current_User\ControlPanel\UserSubKey
If regKey Is Nothing Then Return ""

Return regKey.GetValue(KeyName)

End Function


[quoted text, click to view]
coconet
5/30/2008 10:23:15 AM

I have a list of country names I am using as keys with custom codes as
the values. It currently exists as a text file. I can read it into a
Dictionary. I want to reduce my number of extraneous files and somehow
copy the data to the app.config file.

What you mentioned below would be fine if I wanted to hand-type in all
120 countries and their codes. I don't want to do that. I want to read
and write the Dictionary (or similar Type) to the config file itself.

Thanks.






[quoted text, click to view]
Madhur
5/30/2008 10:56:57 AM
Why not use AppSettings section for this purpose.
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

--
Madhur

[quoted text, click to view]
Jeff Winn
5/31/2008 12:47:53 AM
Well, you can always write a custom configuration section and store whatever
you like in the file:

<configuration>
<configSections>
<add name="myConfigSection"
type="MyApp.Configuration.MyConfigSection, MyApp, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null"/>
</configSections>
<myConfigSection>
<codes>
<add key="1" value="an"/>
<add key="2" value="example"/>
</codes>
</myConfigSection>
<appSettings>...</appSettings>
</configuration>

Basically, you can do whatever you want with the configuration files - just
add a reference to System.Configuration and create a class that derives from
System.Configuration.ConfigurationSection. Note, the version number of my
assembly in the example was 1.0.0.0, the assembly wasn't signed, and i was
assuming the object would be located in the executable just for simplicity.

using System.Configuration;
namespace MyApp.Configuration {
public class MyConfigSection : ConfigurationSection {
public MyConfigSection() {
}

[ConfigurationProperty("codes", IsRequired = false)]
public Dictionary<string, string> Codes {
get { return (Dictionary<string, string>)this["codes"]; }
set { this["codes"] = value; }
}
}
}

That example is more than likely non-working, I was simply trying to show
you how the config file ties to the object you'll be using in your
application.

System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration();
MyConfigSection section =
(MyConfigSection)config.GetSection("myConfigSection");

You'll now have access to the section.Codes property containing your data
you wanted to store in the config file. Just don't forget, the config file
only loads once for the default appdomain. Any changes to it while the
application is executing will not take effect until the appdomain is
reloaded (aka the application is restarted).

HTH

[quoted text, click to view]
coconet
6/5/2008 12:24:10 PM
That works for me.

Thanks!


On Sat, 31 May 2008 00:47:53 -0400, "Jeff Winn" <jwinn@nospam.com>
[quoted text, click to view]
AddThis Social Bookmark Button