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");

            }

  • 相关阅读:
    “程序设计与算法训练”课程设计:“BP神经网络的实现”(C++类封装实现)
    Ubuntu 16.04如何使用无线网卡上网
    Ubuntu 16.04上thunderbird配置163邮箱出现“配置无法被验证-请查看用户名或密码是否正确?”
    合肥工业大学数据结构上机实验代码与实验报告(全)github地址
    Ubuntu 16.04 LTS上git提交出现警告Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts. 的解决方法
    数据结构实验9:图的相关操作
    在Ubuntu 16.04 LTS上用g++和gcc编译C/C++代码错误提示“.../x86_64-linux-gnu/crt1.o: ELF section name out of range”
    pythonlinux配置环境变量
    Tcp和UDP的实现
    值得多看的开源项目
  • 原文地址:https://www.cnblogs.com/lhl123/p/10582917.html
Copyright © 2011-2022 走看看