返回顶部

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

        }

posted @ 2019-03-23 10:54  世界欠绘梨衣一场婚礼  阅读(403)  评论(0编辑  收藏  举报