将 user.config 拆分为不同的文件以加快保存速度(在运行时)

发布于 2024-09-01 03:59:20 字数 1597 浏览 8 评论 0原文

在我的 C# Windows 窗体应用程序 (.net 3.5 / VS 2008) 中,我有 3 个设置文件,生成一个 user.config 文件。

一个设置文件包含较大的数据,但很少更改。经常更改的数据很少。然而,由于设置的保存总是写入整个(XML)文件,所以它总是“慢”。

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

我是否可以以某种方式配置设置以生成两个文件,从而导致:

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

我已经看到可以使用 SecionInformation 类进行进一步自定义,但是对我来说最简单的方法是什么?只需更改 app.config (config.sections) 就可以实现这一点吗?

--- 添加了有关 App.config 的信息

我得到一个文件的原因可能是 App.config 中的 configSections。它看起来是这样的:

  <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>

当我添加第二个和第三个设置文件时,我得到了这些部分。我没有注意到这一点,所以它在某种程度上是VS 2008的默认值。单个user.config有这3个部分,它是绝对透明的。

只是我不知道如何告诉 App.config 创建三个独立的文件而不是一个。我已经“玩弄”了上面的 app.config,但是例如,当我删除配置部分时,我的应用程序会因异常而终止。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失与倦" 2024-09-08 03:59:25

最终我使用了自己的设置提供程序来实现我的目标:
我发现两个最有用的资源开始编写这样的提供程序

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

我使用了1. 的修改版本使用了 2. 中的一些专业知识。我可以为每个设置设置要使用的提供程序,所以我坚持使用这种方法。然而,很长一段时间以来,我一直试图避免编写自己的提供程序,但每当我检查这个主题时,我都发现根本没有更好的方法将这些部分拆分为独立的文件。

不过,我也发现帕特里克的方法很有趣,一旦有时间我就会尝试一下。谢谢你!

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!

毁梦 2024-09-08 03:59:24
If it is slow , instead of doing in single file you can do it in multiple user.config.

注意:但是当我处理 user.settings 时,我发现一个困难,一旦卸载应用程序,设置将不会从保存的位置中删除,并且只有管理权限用户才能访问这些文件。所以请确保您的设置文件是否需要

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

梦年海沫深 2024-09-08 03:59:23

正如您已经发现的,VS 在编译时将项目中的所有配置和设置文件放入一个大的 applicationname.exe.config 文件中,据我所知,您无法加载另一个配置文件作为“主要的一个”。

一种解决方案是拥有自己的设置类实现并让它加载另一个文件。

另一种方法是使用 OpenMappedExeConfiguration 方法。您可以加载配置文件并使用它访问其 appsetting 值。

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);
}

您应该能够获取自定义部分并对其使用 Save 方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文