zoukankan      html  css  js  c++  java
  • c#配置文件appStrings配置节的读取、添加和修改

    程序开发中经常会用到应用程序配置文件,好处就是维护人员可以直接修改配置文件进行维护,而不用修改程序。好,切入主题。

    给项目添加应用程序配置文件App.config,先在里面写几句:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="1" value="内容一"/>
        <add key="2" value="内容二"/>
        <add key="3" value="内容三"/>
      </appSettings>
    </configuration>

      读取内容:老写法是System.Configuration.ConfigurationSettings.AppSettings[“1”].ToString();  这个"1"对应里面的key。
    这里推荐在引用里添加System.configuration,命名空间加入using System.Configuration;

    就可以写成  ConfigurationManager.AppSettings["key"].ToString(); 这里的key就是1、2、3啦。

      添加内容:

     

                Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                cfa.AppSettings.Settings.Add("key", "value");
                //最后调用save
                cfa.Save();
                // 刷新命名节,在下次检索它时将从磁盘重新读取它。记住应用程序要刷新节点
                ConfigurationManager.RefreshSection("appSettings");
                MessageBox.Show("添加成功");

      修改内容:

                Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                cfa.AppSettings.Settings["key名称"].Value = "要改成的value值";
                cfa.Save();
                ConfigurationManager.RefreshSection("appSettings");

      删除内容:

                cfa.AppSettings.Settings.Remove("要删除的key名");
                cfa.Save();

      简单的几个操作就是这些,往大牛们多指教^_^

  • 相关阅读:
    观察OnPaint与OnIdle与OnSize事件
    wxPython的Refresh与事件双重响应
    DLL的静态调用和动态调用
    Delphi String的散漫记录,真是知识无数,陷阱无数
    VC调用Delphi DLL
    终于理解了什么是LGPL
    安装postgresql碰到Unable to write inside TEMP environment path
    图解:Activity生命周期
    Dephi泛型
    传递双重指针申请内存,典型用法
  • 原文地址:https://www.cnblogs.com/wuyouyu/p/3364675.html
Copyright © 2011-2022 走看看