升级应用程序时迁移用户级自定义设置
我有一个使用自定义配置部分进行用户级设置的应用程序。它是通过这种方式完成的,而不是使用设置文件,以便可以将设置集中在程序的单个 app.config 中,而不是像设置文件的默认设置那样每个项目都有一个。
问题是这些自定义设置是每个用户可配置的,并且当用户升级软件时会丢失(为新构建版本创建一个新文件夹,并且不再使用旧文件夹)。因此,我需要一种方法来在更新时或升级后每个用户首次运行应用程序时迁移这些设置。我已经看到了 Settings.Upgrade()< 上的信息/a>,但这仅适用于配置的设置文件部分。我浏览了 ConfigurationSection 的文档(我的自定义设置类/部分从中派生),似乎没有任何帮助。使用 ConfigurationManager 从安装程序中提取用户设置来迁移它们似乎是一个死胡同,因为 OpenExeConfiguration 的重载不支持同时指定可执行文件和用户级别。
有什么提示吗?存储的信息并不重要(主要是记住窗口几何形状和布局设置),但每次应用程序升级时丢失信息可能会很烦人。我唯一能想到的就是完全放弃这些设置的配置机制,并实现我自己的模型,将这些设置存储在用户 AppData 中的版本无关文件中(我在其中存储了一些其他特定于用户的文件)。我希望这是最后一个选择,因为放弃 app.config 包装器会损失很多。
I have an application that uses a custom configuration section for user-level settings. It's done this way instead of using Settings files so that the settings can be centralized in a single app.config for the program, instead of having one per project as is the default for settings files.
The problem is that these custom settings are configurable per-user, and are lost when the user upgrades the software (a new folder for the new build version is created, and the old folder is no longer used). So, I need a way to migrate these settings either on update, or on the first run of the application per user after the upgrade. I've seen the info on Settings.Upgrade(), but that only works specifically on the Settings file section of the config. I've looked through documentation for ConfigurationSection (from which my custom settings class/section derives) and there doesn't seem to be anything helpful. Using ConfigurationManager to pull in the user settings from the installer to migrate them seems to be a dead-end, as the overloads of OpenExeConfiguration don't support specifying both the executable and the user level.
Any hints? The information stored is not critical (remembered window geometry and layout settings, mostly) but can be annoying to lose every time the app is upgraded. The only thing I can think of is to forego the config mechanism for these settings altogether, and implement my own model for storing these settings in version-agnostic files in the user's AppData (where I store a few other user-specific files). I would prefer that to be the last option, as you lose a lot by giving up the app.config wrappers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要创建对外部配置节文件的引用,该引用在更新期间保持不变。使用 configSource 属性来引用该文件。
这样,用户可以在 app.config 更新时保留其设置。
这是相关帖子:如何启用 configSource .NET 中自定义配置节的属性?
You might want to create a reference to an external configuration section file that stays in tact during an update. Use the configSource attribute to reference the file.
That way users can keep their settings when the app.config is updated.
This is a related post: How to enable configSource attribute for Custom Configuration Section in .NET?
这是一个老问题,但我会写一个答案,以防有人通过谷歌最终到达这里。
我通过强制记住以前版本的文件路径并用旧文件覆盖新配置位置中的文件来“解决”这个问题。
我确信这种方法有很多缺陷,但如果您正在寻找可能有效的快速解决方案,那么您可能会发现这很有用。
This is an old question but I'll write an answer in case anyone ends up here via Google.
I "solved" this issue by forcefully remembering the previous version's file path and overriding the file in the new config location with the old file.
I'm sure there are many flaws with this approach but if you're looking for a quick solution that might work then you may find this useful.