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

            }

  • 相关阅读:
    记一次 .NET 某智能服装智造系统 内存泄漏分析
    记一次 .NET 某化妆品 webapi 卡死分析
    记一次 .NET 某公交卡扣费系统 程序卡死分析
    去掉烦人的:要恢复页面吗?Chrome未正确关闭
    C#Excel转图片代码
    ArcEngine实现pagelayout中文本元素的属性对话框
    arcgis 模型版本问题最大
    Arcengine开发所遇错误解决方案(持续更新)
    ArcEngine IPageLayout 添加经纬网和公里网
    Arcengine的复制粘贴
  • 原文地址:https://www.cnblogs.com/lhl123/p/10582917.html
Copyright © 2020-2023  润新知