C#中使用设置(Settings.settings) Properties.Settings.Default
应用程序及用户设置
在设计时创建新设置的步骤
在“Solution Explorer”(解决方案资源管理器)中,展开项目的“Properties”(属性)节点。
在“Solution Explorer”(解决方案资源管理器)中,双击要在其中添加新设置的 .settings 文件。此文件的默认名称是 Settings.settings。
在应用程序会话之间更改设置值
有时,在编译和部署了应用程序后,可能需要在应用程序会话之间更改设置值。例如,可能需要更改连接字符串,使其指向正确的数据库位置。由于设计时工具在应用程序编译和部署后不可用,所以必须手动在文件中更改设置值。
在应用程序会话间更改设置值的步骤
| • |
使用 Microsoft 记事本或其他某种文本或 XML 编辑器,打开与应用程序关联的 <AssemblyName>.exe.config 文件。(该文件在Bin文件夹下的Debug或Release文件夹下) |
| • |
找到要更改的设置条目。它应看似以下示例: <setting name="Setting" serializeAs="String"> |
| • |
为设置键入新值,然后保存该文件。 |
在运行时使用设置
运行时应用程序可以通过代码使用设置。具有应用程序作用域的设置值能够以只读方式进行访问,而用户作用域设置的值可以进行读写。在 C# 中可以通过 Properties 命名空间使用设置。
在运行时读取设置
可在运行时使用 Properties 命名空间读取应用程序作用域及用户作用域设置。Properties 命名空间通过Properties.Settings.Default 对象公开了项目的所有默认设置。编写使用设置的代码时,所有设置都会出现在 IntelliSense 中并且被强类型化。因此,举例来说,如果设置的类型为 System.Drawing.Color,则无需先对其进行强制类型转换即可使用该设置,如下例所示:
this.BackColor = Properties.Settings.Default.myColor;
在运行时保存用户设置
应用程序作用域设置是只读的,只能在设计时或通过在应用程序会话之间修改 <AssemblyName>.exe.config 文件来进行更改。然而,用户作用域设置却可以在运行时进行写入,就像更改任何属性值那样。新值会在应用程序会话持续期间一直保持下去。可以通过调用 Settings.Save 方法来保持在应用程序会话之间对用户设置所做的更改。这些设置保存在 User.config 文件中。
在运行时写入和保持用户设置的步骤
| • |
访问用户设置并为其分配新值,如下例所示: Properties.Settings.Default.myColor = Color.AliceBlue;
|
| • |
如果要保持在应用程序会话之间对用户设置所做的更改,请调用 Save 方法,如以下代码所示: Properties.Settings.Default.Save(); |
C#中使用设置(Settings.settings) Properties.Settings.Default的更多相关文章
- C#中使用设置(Settings.settings) Properties.Settings.Default .(配置文件相当重要)
C#中使用设置(Settings.settings) Properties.Settings.Default . 2016年08月04日 15:02:43 zxd9790902 阅读数:10664更多 ...
- The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...
- 【转】The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
[转]The content of element type "configuration" must match "(properties?,settings?,typ ...
- C# winform或控制台Properties.Settings.Default的使用及存储位置
C# winform或控制台Properties.Settings.Default的使用及存储位置 作者的程序 是MmPS.ClientForm.exe,使用Properties.Settings.D ...
- WPF 应用 - 使用 Properties.Settings 保存客户端密码
1. 先在项目的 Settings.settings 新建需要的字段和类型 有需要还可设置初始默认值 2. 启动客户端时,获取 Properties.Settings 的属性值 public void ...
- The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProv
在mybatis配置文件config.xml中报错: The content of element type "configuration" must match "(p ...
- mybatis:"configuration" must match "(properties?,settings?,typeAliase.....
在运行mybatis配置文件的时候,出现错误: mybatis:"configuration" must match "(properties?,settings?,ty ...
- 无法识别的配置节点 applicationSettings/* Properties.Settings 解决方法
http://blog.csdn.net/yaoxtao/article/details/7766888 在项目中引用web service时,偶然出现 无法识别的配置节点 applicationSe ...
- 元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?
报错主要部分如下: Error building SqlSession.### Cause: org.apache.ibatis.builder.BuilderException: Error cre ...
随机推荐
- Django之深入了解视图层
目录 视图层三板斧 HttpResponse render redirect JsonResponse FBV CBV CBV源码 如何给FBV和CBV加装饰器 视图层三板斧 规定视图函数必须有一个返 ...
- 洛谷P3376【模板】网络最大流 Dinic模板
之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...
- 通过Amazon AWS 十分钟搭建私人主机 自由的不要不要的
首先承认有点标题党了,当时自己搞的时候可不止十分钟,好吧,我承认是坑太多了,所以特意开了一篇博文,就是要准备尝试的和我一样的菜鸟们,可以真正的十分钟搞定. 当然高手可能用不上十分钟. 首先,就是 ...
- Censoring【自动AC机】【水题毁我青春】【20190614】
这题简直比注水猪肉还水QAQ. 以前做过KMP的Censoring单串匹配,果断选择自动AC机w 对短串建自动AC机 长串去机子里匹配 用个栈边匹配边弹出 记得弹出一个串后把匹配点指向栈顶就ojbk ...
- Windows Server 2008 R2 部署服务
Windows Server 2008 R2 部署服务 部分参考: Windows Server 2008 R2 部署服务 - 马睿的技术博客 - 51CTO技术博客http://marui.blog ...
- Ubuntu 安装gnome桌面及vnc远程连接
安装gnome桌面 sudo apt-get install gnome-core 安装vnc sudo apt-get install vnc4server 启动vnc vncserver 设置一下 ...
- PAT甲级——A1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT甲级——A1008 Elevator
The highest building in our city has only one elevator. A request list is made up with N positive nu ...
- linux查看系统内容
- Python学习day24-面向对象的三大特征之继承
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...