C#中使用设置(Settings.settings) Properties.Settings.Default .

2016年08月04日 15:02:43 zxd9790902 阅读数:10664更多

个人分类: c#
 https://blog.csdn.net/ZXD9790902/article/details/52119340

在设计时创建新设置的步骤

在“Solution Explorer”(解决方案资源管理器)中,展开项目的“Properties”(属性)节点。

在“Solution Explorer”(解决方案资源管理器)中,双击要在其中添加新设置的 .settings 文件。此文件的默认名称是 Settings.settings。

为设置键入新值,然后保存该文件。

在运行时使用设置

运行时应用程序可以通过代码使用设置。具有应用程序作用域的设置值能够以只读方式进行访问,而用户作用域设置的值可以进行读写。在 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();

=========================================================================================================================================================================================================================================================================================================================================================================================================

1、定义

在Settings.settings文件中定义配置字段。把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改。可以使用数据网格视图,很方便;

2、读取配置值

text1.text = Properties.Settings.Default.FieldName;
//FieldName是你定义的字段

3、修改和保存配置

Properties.Settings.Default.FieldName = "server";

Properties.Settings.Default.Save();//使用Save方法保存更改

4、也可以自己创建

创建一个配置类FtpSetting。在WinForm应用程序里,一切配置类都得继承自 ApplicationSettingsBase 类。

sealed class FtpSettings : ApplicationSettingsBase

{
[UserScopedSetting]
[DefaultSettingValue("127.0.0.1")]
public string Server
{
get { return (string)this["Server"]; }
set { this["Server"] = value; }
}
[UserScopedSetting]
[DefaultSettingValue("21")]
public int Port
{
get { return (int)this["Port"]; }
set { this["Port"] = value; }
}
}

使用上述配置类,可以用:

private void button2_Click(object sender, EventArgs e)
{
FtpSettings ftp = new FtpSettings();
string msg = ftp.Server + ":" + ftp.Port.ToString();
MessageBox.Show(msg);
}

我们在使用上述FtpSetting 配置时,当然要先进行赋值保存,然后再使用,后面再修改,再保存,再使用。
private void button2_Click(object sender, EventArgs e)
{
FtpSettings ftp = new FtpSettings();
ftp.Server = "ftp.test.com";
ftp.Port = 8021;
ftp.Save();
ftp.Reload();
string msg = ftp.Server + ":" + ftp.Port.ToString();
MessageBox.Show(msg);
}
嗯。已经Save了,你可能会在应用程序文件夹里找不到它到底保存到哪里去了。由于我们是用UserScope的,所以其实该配置信息是保存到了你的Windows的个人文件夹里去了。比如我的就是 C:\Documents and Settings\brooks\Local Settings\Application Data\TestWinForm目录了。

=========================================================================================================================================================================================================================================================================================================================================================================================================

例:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Configuration;

namespace 设置文件读写测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.name = this.textBox1.Text;
            Properties.Settings.Default.Save();
            this.label1.Text = Properties.Settings.Default.name;
        }

private void button2_Click(object sender, EventArgs e)
        {
            this.label1.Text = Properties.Settings.Default.name;
        }

private void button3_Click(object sender, EventArgs e)
        {
            FtpSettings ftp = new FtpSettings();
            string msg = ftp.Server + ":" + ftp.Port.ToString();
            this.label2.Text = msg;
        }

private void button4_Click(object sender, EventArgs e)
        {
            FtpSettings ftp = new FtpSettings();
            ftp.Server = this.textBox2.Text ;
            ftp.Port = Convert.ToInt32(this.textBox3.Text);
            ftp.Save();
            //ftp.Reload();
            string msg = ftp.Server + ":" + ftp.Port.ToString();
            this.label2.Text = msg;
        }
    }

sealed class FtpSettings : ApplicationSettingsBase
    {
        [UserScopedSetting]
        [DefaultSettingValue("127.0.0.1")]
        public string Server
        {
            get { return (string)this["Server"]; }
            set { this["Server"] = value; }
        }
        [UserScopedSetting]
        [DefaultSettingValue("21")]
        public int Port
        {
            get { return (int)this["Port"]; }
            set { this["Port"] = value; }
        }
    }
}

C#中使用设置(Settings.settings) Properties.Settings.Default .(配置文件相当重要)的更多相关文章

  1. C#中使用设置(Settings.settings) Properties.Settings.Default

    应用程序及用户设置 在设计时创建新设置的步骤 在“Solution Explorer”(解决方案资源管理器)中,展开项目的“Properties”(属性)节点. 在“Solution Explorer ...

  2. The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...

  3. 【转】The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...

    [转]The content of element type "configuration" must match "(properties?,settings?,typ ...

  4. C# winform或控制台Properties.Settings.Default的使用及存储位置

    C# winform或控制台Properties.Settings.Default的使用及存储位置 作者的程序 是MmPS.ClientForm.exe,使用Properties.Settings.D ...

  5. WPF 应用 - 使用 Properties.Settings 保存客户端密码

    1. 先在项目的 Settings.settings 新建需要的字段和类型 有需要还可设置初始默认值 2. 启动客户端时,获取 Properties.Settings 的属性值 public void ...

  6. 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 ...

  7. mybatis:"configuration" must match "(properties?,settings?,typeAliase.....

    在运行mybatis配置文件的时候,出现错误: mybatis:"configuration" must match "(properties?,settings?,ty ...

  8. 无法识别的配置节点 applicationSettings/* Properties.Settings 解决方法

    http://blog.csdn.net/yaoxtao/article/details/7766888 在项目中引用web service时,偶然出现 无法识别的配置节点 applicationSe ...

  9. 元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?

    报错主要部分如下: Error building SqlSession.### Cause: org.apache.ibatis.builder.BuilderException: Error cre ...

随机推荐

  1. ES查询不重复的数据

    GET ana-apk/_search #查询不重复的mac地址{  "size": 10,  "aggs": {    "MAC": {  ...

  2. pycharm 右键无法显示unittest框架&&解决右键只有unittest 运行如何取消右键显示进行普通run

    上面是普通文件和unittest 导入的文件右键快捷键显示情况,可以看出两者快捷键都是ctr+shift+F10,如果你是右键模式想运行unitest,但是又不知道哪里配置unittest直接运行快捷 ...

  3. 自定义Redis作为Session存储服务提供

    之前看网上介绍可使用Redis自定义Session托管,使用第三方的Harbour.RedisSessionStateStore GitHub:https://github.com/TheCloudl ...

  4. JavaScript图形实例:圆内螺线

    数学中有各式各样富含诗意的曲线,螺旋线就是其中比较特别的一类.螺旋线这个名词来源于希腊文,它的原意是“旋卷”或“缠卷”.例如,平面螺旋线便是以一个固定点开始向外逐圈旋绕而形成的曲线. 阿基米德螺线和黄 ...

  5. vmware fusion nat网络模式设置固定ip

    最近想在本地用虚拟环境搭一个k8s环境,但是发现虚拟机的ip会不定时自动变化,导致mosh客户端连接经常中断.于是就想让虚拟机的ip固定住,不再变动. mac 上的 vmware fusion 设置固 ...

  6. 使用SQL语句还原数据库 2012.3.20

    --返回由备份集内包含的数据库和日志文件列表组成的结果集. --主要获得逻辑文件名 USE master RESTORE FILELISTONLY FROM DISK = 'g:\back.Bak' ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:按键提示

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Linux centosVMware Linux监控平台介绍、zabbix监控介绍、安装zabbix、忘记Admin密码如何做

    一.Linux监控平台介绍 cacti.nagios.zabbix.smokeping.open-falcon等等 cacti.smokeping偏向于基础监控,成图非常漂亮 cacti.nagios ...

  9. Windows下载编译Qt5 Gui

    安装工具 Python 这个安装的时候没记录下来,网上查一下,大把, 就不补了. ActivePerl https://www.cnblogs.com/dilex/p/10591579.html 下载 ...

  10. Flask - 中间件

    其实就是封装旧酒,装进新瓶,自己再加点料.留坑,还没有用到. Flask的请求扩展就是Django的中间件.Django的中间件不是Flask的中间件 from flask import Flask ...