Split user.config into different files for faster economy (at runtime)
In my C # Windows Forms application (.net 3.5 / VS 2008) I have 3 settings files resulting in one user.config file.
A single configuration file consists of larger data, but rarely changes. Often very little data has changed. However, since saving settings always writes the entire (XML) file, it is always "slow".
SettingsSmall.Default.Save(); // slow, even if SettingsSmall consists of little data
Can I tweak the options somehow to get two files, resulting in:
SettingsSmall.Default.Save(); // should be fast
SettingsBig.Default.Save(); // could be slow, is seldom saved
I saw that I can use the SecionInformation class to further customize, however what would be the easiest way for me? Is this possible by just changing app.config (config.sections)?
--- added information about App.config
The reason I can get one file might be the configSections in App.config. This is how it looks:
<configSections>
<sectionGroup name = "userSettings" type = "System.Configuration.UserSettingsGroup, System, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089">
<section name = "XY.A.Properties.Settings2Class" type = "System.Configuration.ClientSettingsSection, System, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089" allowExeDefinition = "MachineToLocalUser" requirePermission = "false" />
<section name = "XY.A.Properties.Settings3Class" type = "System.Configuration.ClientSettingsSection, System, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089" allowExeDefinition = "MachineToLocalUser" requirePermission = "false" />
</sectionGroup>
</configSections>
I got the sections when I added the 2nd and 3rd settings files. I didn't pay attention to it, so it was somehow the default of VS 2008. The only user.config has these 3 sections, it is completely transparent.
Only I don't know how to tell App.config to create three independent files instead of one. I've played around with the app.config above, but for example, when I remove the configuration sections, my applications exit with an exception.
a source to share
If it is slow , instead of doing in single file you can do it in multiple user.config.
Note. But when I worked on user.settings, I found one difficulty. Once the app is uninstalled, the settings will not remove the saved location form, nor only the custom permission that the user can access. So please make sure your configuration files are required or not.
a source to share
As you already found out, VS translates all the config and settings files in your project into one big file applicationname.exe.config
when compiling, and as far as I know, you cannot load another config file as the "main" one.
One solution is to create your own implementation of the settings class and load another file.
An alternative to this could be using the OpenMappedExeConfiguration method in the ConfigurationManager. You can load the config file and access its appsetting values using
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "test.config";
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = ((AppSettingsSection)configuration.GetSection("appSettings")).Settings;
foreach (string key in section.AllKeys) {
Console.WriteLine("{0}={1}", key, section[key].Value);
}
You should be able to get a special section and use the Save method on it.
a source to share
In the end, I used my own settings provider to accomplish my goal: I found that two resources are most useful for getting started with such a provider.
- www.codeproject.com/KB/vb/CustomSettingsProvider.aspx?msg=2934144#xx2934144xx
- www.blayd.co.uk/download.aspx?pageid=1013
I used modified version 1. Using some know-how in 2. I can set for each setting which vendor to use, so I take this approach. However, I tried to avoid writing my provider for a long time, but whenever I checked this question, I found that not the best approach for splitting partitions into independent files.
However, I also think Patrick's approach is interesting and will put him to the test as soon as I have time. Thanks!
a source to share