zoukankan      html  css  js  c++  java
  • C#—读写App.config

    :使用ConfigurationManager

    :添加System.configguration

     

    :引用空间

     

    :App.config配置文件配置节

    A:自定义配置

     

    B:数据源配置

    <connectionStrings>

       <addname="kyd" connectionString="server=.;database=UFDATA_999_2017;user=sa;pwd=123"/>

    </connectionStrings>

    C:普通配置

    <appSettings>  

       <add key="COM1" value="COM1,9600,8,None,1,已启用" />

    </appSettings>

    :Config文件读写

    1.依据连接串名返回字符串

    A:界面

     

    B:后台代码

     private void button1_Click(object sender, EventArgs e)

            {

                //指定config文件读取

                string file = System.Windows.Forms.Application.ExecutablePath;

                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file);

                string connectionString =

                    config.ConnectionStrings.ConnectionStrings["sql1"].ConnectionString.ToString();

                MessageBox.Show( connectionString);

            }

    2.更新连接字符串

    A:界面

     

    B:后台代码

    private void button2_Click(object sender, EventArgs e)

            {

                ///<summary> 

                ///更新连接字符串  

                ///</summary> 

                ///<param name="newName">连接字符串名称</param> 

                ///<param name="newConString">连接字符串内容</param> 

                ///<param name="newProviderName">数据提供程序名称</param> 

                    //指定config文件读取

                    string file = System.Windows.Forms.Application.ExecutablePath;

                    Configuration config = ConfigurationManager.OpenExeConfiguration(file);

                    bool exist = false; //记录该连接串是否已经存在  

                                        //如果要更改的连接串已经存在  

                    if (config.ConnectionStrings.ConnectionStrings["sql1"] != null)

                    {

                        exist = true;

                    }

                    // 如果连接串已存在,首先删除它  

                    if (exist)

                    {

                        config.ConnectionStrings.ConnectionStrings.Remove("sql1");

                    }

                    //新建一个连接字符串实例  

                    ConnectionStringSettings mySettings =

                        new ConnectionStringSettings("sql1", "Data","AA");

                    // 将新的连接串添加到配置文件中.  

                    config.ConnectionStrings.ConnectionStrings.Add(mySettings);

                    // 保存对配置文件所作的更改  

                    config.Save(ConfigurationSaveMode.Modified);

                    // 强制重新载入配置文件的ConnectionStrings配置节  

                    ConfigurationManager.RefreshSection("connectionStrings");

                }

    3.exe.confing文件中的appSettings配置节的value

    A:界面

     

    B:后台代码

    private void button3_Click(object sender, EventArgs e)

            {

                string file = System.Windows.Forms.Application.ExecutablePath;

                Configuration config = ConfigurationManager.OpenExeConfiguration(file);

                foreach (string key in config.AppSettings.Settings.AllKeys)

                {

                    if (key == "key1")

                    {

                         MessageBox.Show(config.AppSettings.Settings["key1"].Value.ToString());

                    }

                }

            }

    4.exe.confing文件中的appSettings配置节增加一对键值对

    A:界面

     

    B:后台代码

     private void button4_Click(object sender, EventArgs e)

            {

                string file = System.Windows.Forms.Application.ExecutablePath;

                Configuration config = ConfigurationManager.OpenExeConfiguration(file);

                bool exist = false;

                foreach (string key in config.AppSettings.Settings.AllKeys)

                {

                    if (key == "key1")

                    {

                        exist = true;

                    }

                }

                if (exist)

                {

                    config.AppSettings.Settings.Remove("key1");

                }

                config.AppSettings.Settings.Add("key1", "5555");

                config.Save(ConfigurationSaveMode.Modified);

                ConfigurationManager.RefreshSection("appSettings");

            }

  • 相关阅读:
    UITableViewCell分隔线
    Swift:Debug和Release状态下错误输出
    开发中遇到的那些坑
    code sign error 1
    Xcode头文件加锁
    UIPageControl显示图片
    关于RTKLIB资料整理和学习
    I2C中24C02从地址设置
    从库函数操作RCC的流程来理解偏移变量
    对于STM32别名区的理解 (转载)
  • 原文地址:https://www.cnblogs.com/lhl123/p/10582917.html
Copyright © 2011-2022 走看看