最佳实践来保存应用程序设置在Windows窗体应用程序 [英] Best practice to save application settings in a Windows Forms Application

查看:201
本文介绍了最佳实践来保存应用程序设置在Windows窗体应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现很简单:我有一个Windows窗体(.NET 3.5)应用程序,使用读取信息的路径。这个路径可以由用户进行修改,通过使用选项形式I提供

What I want to achieve is very simple: I have a Windows Forms (.NET 3.5) application that uses a path for reading information. This path can be modified by the user, by using the options form I provide.

现在,我想的路径值保存到以后使用的文件。这将是保存到这个文件的许多设置之一。该文件将在应用程序文件夹直接坐。

Now, I want to save the path value to a file for later use. This would be one of the many settings saved to this file. This file would sit directly in the application folder.

我的理解有三种选择:


  • ConfigurationSettings文件(appname.exe.config)

  • 注册

  • 自定义XML文件

我读到了.NET配置文件没有预见保存值回吧。
至于注册表,我希望得到尽可能远离它越好。

I read that the .NET configuration file is not foreseen for saving values back to it. As for the registry, I would like to get as far away from it as possible.

这是否意味着我应该使用自定义XML文件来保存配置设置?
如果是这样,我想看到​​(C#)的code的例子。

Does this mean that I should use a custom XML file to save configuration settings? If so, I would like to see code example of that (C#).

我已经看到了关于这个问题的其他讨论,但它仍然是我也不清楚。

I have seen other discussions on this subject, but it is still not clear to me.

推荐答案

如果您使用Visual Studio工作,那么它是pretty容易得到持久化设置。右键单击Solution Explorer中的项目,选择属性。选择设置选项卡,单击超链接,如果不存在设置。使用设置标签来创建应用程序设置。 Visual Studio中创建的文件 Settings.settings Settings.Designer.settings 包含单例类设置从<一个继承href=\"http://msdn.microsoft.com/en-us/library/system.configuration.applicationsettingsbase.aspx\">ApplicationSettingsBase.您可以从code访问该类读/写应用程序设置:

If you work with Visual Studio then it is pretty easy to get persistable settings. Right click on the project in Solution Explorer, choose Properties. Select the Settings tab, click on the hyperlink if settings doesn't exist. Use the Settings tab to create application settings. Visual Studio creates the files Settings.settings and Settings.Designer.settings that contain the singleton class Settings inherited from ApplicationSettingsBase. You can access this class from your code to read/write application settings:

Properties.Settings.Default["SomeProperty"] = "Some Value";
Properties.Settings.Default.Save(); // Saves settings in application configuration file

此技术适用这两个控制台,Windows Forms和其他项目类型。

This technique is applicable both for console, Windows Forms and other project types.

请注意,您需要设置的范围的属性设置。如果您选择了适用范围,然后Settings.Default&LT;你的财产>将是只读的。

Note that you need to set the scope property of your settings. If you select Application scope then Settings.Default.< your property > will be read-only.

这篇关于最佳实践来保存应用程序设置在Windows窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆