如何将应用程序设置保存到文件?
我有简单的应用程序。更具体地说,这是 Visual Studio Addin。 客户端需要指定几个设置(字符串、整数、十进制等),
我需要能够在启动时加载它并在更改时保存。
为简单起见,我将所有设置都放在一类 AppSettings 中。
我当然可以使用 DataContractSerializer 来序列化/反序列化 AppSettings 但 我认为必须有一些标准的方法。
I have simple application. To be more specific this is Visual Studio Addin.
Client needs to specify several settings (string, int, decimal etc)
I need to be able to load it at starup time and save if changed.
Let's say for simplicity I have all settings in one class AppSettings.
I can of course use DataContractSerializer to serialize/deserialize AppSettings but
I think there must be some standard way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有来自 Microsoft 的应用程序设置:
http://msdn.microsoft.com/en-us/library/a65txexh.aspx ?appId=Dev10IDEF1&l=EN-US&k=k(APPLICATIONSETTINGSOVERVIEW);k(TargetFrameworkMoniker-%22.NETFRAMEWORK&k=VERSION=V4.0%22)&rd=true
你可以使用它像这样:
加载工作类似。但您必须首先定义一个 .settings 文件。它来自 WPF 框架,不确定它是否也适用于 WinForms。
类似的问题有类似的答案:
在 Windows 窗体应用程序中保存应用程序设置的最佳实践< /a>
There are Application Settings from Microsoft:
http://msdn.microsoft.com/en-us/library/a65txexh.aspx?appId=Dev10IDEF1&l=EN-US&k=k(APPLICATIONSETTINGSOVERVIEW);k(TargetFrameworkMoniker-%22.NETFRAMEWORK&k=VERSION=V4.0%22)&rd=true
You can use it like this:
And loading works similar. But you have to define a .settings file first. It's from the WPF Framework, not sure if it works for WinForms too.
Similar question with a similar answer:
Best practice to save application settings in a Windows Forms Application
我本人非常喜欢平面文件存储,例如 INI 文件。可以在此处<找到一篇文章,其中包含有关如何在 .NET 中使用 INI 文件的示例代码/a>.
I'm a huge fan of flat file storage myself, for example INI files. An article with example code on how to use INI files with .NET can be found here.
Visual Studio Addin 是一个特例。在这种情况下,如果您想使用标准安装插件包。当我遇到这样的问题时,我尝试使用 Windows 注册表来解决它。您可以找到很多如何在 C# 中处理 Windows 注册表的示例。
Visual Studio Addin is specific case. In this case we can distribute only a .dll file which contain add in code and some .xml with Add-in definition if you want to use standard installation Add-In package. When I had such problem I try to resolve it with Windows registry. You can find a lot of examples how to deal with Windows registry in c#.