views:

78

answers:

3

In my c# Windows Forms application (.net 3.5 / VS 2008) I have 3 settings files resulting in one user.config file.

One setting file consists of larger data, but is rarely changed. The frequently changed data are very few. However, since the saving of the settings is always writing the whole (XML) file it is always "slow".

SettingsSmall.Default.Save(); // slow, even if SettingsSmall consists of little data 

Could I configure the settings somehow to result in two files, resulting in:

SettingsSmall.Default.Save(); // should be fast
SettingsBig.Default.Save(); // could be slow, is seldom saved

I have seen that I can use the SecionInformation class for further customizing, however what would be the easiest approach for me? Is this possible by just changing the app.config (config.sections)?

--- added information about App.config

The reason why I get one file might be the configSections in the 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've added the 2nd and 3rd settings file. I have not paid any attention to this, so it was somehow the default of VS 2008. The single user.config has these 3 sections, it is absolutely transparent.

Only I do not know how to tell the App.config to create three independent files instead of one. I have "played around" with the app.config above, but e.g. when I remove the config sections my applications terminates with an exception.

A: 
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 application is uninstalled the settings wont remove form the saved location as well as only administrative permission user can access those files. So please make sure that your setting files in needed or not

anishmarokey
Thanks for your hint, what I miss is how I get multiple user.config files. My settings only result in one and I have no idea where to set up that it is stored in different files. This is exactly what i need to know...
HorstWalter
add two settings file to your application.It automatically creates necessary settings in the config file. If its not creating let me know
anishmarokey
In my case I have 3 settings file, but the actual data will be stored in one single file. I have added information about my situation above. Thanks so far for your feedback.
HorstWalter
A: 

As you have already discovered, VS puts all config and settings files in your project into one big applicationname.exe.config file when compiled and as far as I know you can not load another config file as the "main one".

One solution is to have your own implementation of a settings class and have it load another file.

An alternative to this could be to use the OpenMappedExeConfiguration method in ConfigurationManager.. You can load a 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 custom section and use the Save method on it as well.

Patrick
A: 

Eventually I have used my own settings provider in order to achieve my goal: I found two resource most useful getting started writing such a provider

  1. www.codeproject.com/KB/vb/CustomSettingsProvider.aspx?msg=2934144#xx2934144xx
  2. www.blayd.co.uk/download.aspx?pageid=1013

I have used a modified version of 1. using some of the know-how in 2. I can set for each setting which provider to use, so I stick with that approach. However, i have tried to avoid writing my own provider for a long time, but whenever I checked on this topic, i have found not better approach at all to split the sections into independent files.

However, I also find Patrick's approach interesting and will give it a trial as soon I have the time. Thank you!

Horst Walter