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

在“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"(属性)节点. ...

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

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

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

    在运行mybatis配置文件的时候,出现错误: Caused by: org.xml.sax.SAXParseException: The content of element type " ...

  4. 2020年:maven配置最新阿里云镜像,以及在IDEA中的设置

    记得当初学习Maven的时候,由国外的中央仓库切换为阿里云镜像之后,用起来是辣么地丝滑~ 不过最近一段时间,Maven却总是出现一些问题,本地库里也总是出现一些.lastUpdated文件,类似于下面 ...

  5. SpringBoot Application.yml 中可设置的属性导航

    原文链接:SpringBoot 可设置的属性 # ---------------------------------------- #核心特性 # -------------------------- ...

  6. android studio中如何设置注释模板

    在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android studio中该如何设置呢? 工具/原料 android ...

  7. 时序约束基础 和 quartusII 中的设置

    时序约束目的: 一.提高设计的工作频率 二.获得正确的时序分析报告(STA:静态时序分析) 常用的时序概念:   周期,    最大时钟频率.    时钟建立时间.时钟保持时间.    时钟到输出延时 ...

  8. 在Python中使用设置文件的最佳做法是什么? [关闭]

    本文翻译自:What's the best practice using a settings file in Python? [closed] I have a command line scrip ...

  9. 如何在Windows 10中打开设置?

    Windows 10 provides the Windows Settings in order to configure different settings of the Windows 10. ...

最新文章

  1. NAACL | 通过对抗性修改,探究链接预测的鲁棒性和可解释性
  2. TCP/IP协议:概述
  3. Python输出py文件模拟代码高亮
  4. linux使用nginx负载udp
  5. 成功解决ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
  6. dbcp连接mysql,8小时会自动断开连接
  7. Java 修饰符、运算符笔记总结
  8. Tomcat容器、JSP和Servlet
  9. JS对象的属性名规则
  10. 期权价格的上限和下限
  11. TensorFlow和Keras入门必读教程
  12. Nodejs之旅开始
  13. 【Kafka】Kafka如何开启SSL 控制台消费与生产 代码消费与生产
  14. 猜数游戏--MOOC中习题
  15. camerax_Android CameraX概述
  16. 1.1 OC类的认识
  17. asp.net高校宿舍后勤管理系统案例
  18. oracle索引有哪些分类,Oracle中的索引分类
  19. Wireshark教程:识别主机和用户
  20. android 车牌字符分割,车牌识别 之 字符分割

热门文章

  1. 全新版的TFN 光时域反射仪上线了,大家一块了解一下吧。
  2. STM32无线通信——nRF24L01通信模块
  3. 通过root权限可直接修改Ubuntu用户密码(需要知道root密码)
  4. fgets 函数详解
  5. 个人晋升演讲ppt_关于升职演讲ppt演讲范文及制作要求
  6. [转]String 之 new String()和 intern()方法深入分析
  7. 视觉工程师面试50问
  8. C++:堆和栈的理解
  9. b插到元素a之前python_python面试题(转)
  10. ubuntu16.4离线安装显卡驱动