开发者

Split user.config into different files for faster saving (at runtime)

开发者 https://www.devze.com 2022-12-30 06:49 出处:网络
In my c# Windows Forms application (.net 3.5 / VS 2008) I have 3 settings files resulting in one user.config file.

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.


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.


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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消