如何在Windows窗体应用程序中保存应用程序设置? [英] How can I save application settings in a Windows Forms application?

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

问题描述

我想要实现的非常简单:我有一个Windows Forms(.NET 3.5)应用程序,该应用程序使用读取信息的路径。用户可以使用我提供的选项表单来修改此路径。



现在,我要将路径值保存到文件中以备后用。这将是保存到该文件的众多设置之一。该文件将直接位于应用程序文件夹中。



我知道可以使用三个选项:




  • ConfigurationSettings文件(appname.exe.config)

  • 注册表

  • 自定义XML文件



我读到没有预见到.NET配置文件会将值保存回它。至于注册表,我想尽可能地远离它。



这是否意味着我应该使用自定义XML文件来保存配置设置? / p>

如果是这样,我想查看该代码示例(C#)。



我还看到了其他讨论

解决方案

如果您使用Visual Studio,则很容易获得持久设置。在解决方案资源管理器中右键单击该项目,然后选择属性。选择设置标签,如果设置不存在,请单击超链接。



使用设置标签创建应用程序设置。 Visual Studio创建文件 Settings.settings Settings.Designer.settings 包含单例类设置继承自 ApplicationSettingsBase 。您可以从代码中访问此类以读取/写入应用程序设置:

  Properties.Settings.Default [ SomeProperty] = 某些价值; 
Properties.Settings.Default.Save(); //将设置保存在应用程序配置文件

此技术适用于控制台,Windows窗体和其他项目类型。



请注意,您需要设置设置的 scope 属性。如果选择应用程序作用域,则Settings.Default。 <您的属性>将为只读。



参考:如何:在运行时写入用户设置C#-Microsoft文档


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.

I understand three options are available:

  • ConfigurationSettings file (appname.exe.config)
  • Registry
  • Custom XML file

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.

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.

解决方案

If you work with Visual Studio then it is pretty easy to get persistable settings. Right click on the project in Solution Explorer and choose Properties. Select the Settings tab and 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

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

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.

Reference: How To: Write User Settings at Run Time with C# - Microsoft Docs

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

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