zoukankan      html  css  js  c++  java
  • C# 手动读写app config 的源码

     public class ConfigOperator
     {
     public string strFileName;
     public string configName;
     public string configValue;
     public ConfigOperator()
     {
     //
     // TODO: 在此处添加构造函数逻辑
     //
     }

     public string ReadConfig1(string configKey)
     {
     configValue = "";
     configValue = ConfigurationSettings.AppSettings[""+configKey+""];
     return configValue;
     }
     
    //得到程序的config文件的名称以及其所在的全路径
     public void SetConfigName(string strConfigName)
     {
     configName = strConfigName;
     //获得配置文件的全路径
     GetFullPath();
     }

     public void GetFullPath()
     {
     //获得配置文件的全路径
     strFileName=AppDomain.CurrentDomain.BaseDirectory.ToString()+configName;
     }

     public void SaveConfig(string configKey,string configValue)
     {
     XmlDocument doc=new XmlDocument();
     doc.Load(strFileName);
     //找出名称为“add”的所有元素
     XmlNodeList nodes=doc.GetElementsByTagName("add");
     for(int i=0;i<nodes.Count;i++)

     {
     //获得将当前元素的key属性
     XmlAttribute att=nodes[i].Attributes["key"];
     //根据元素的第一个属性来判断当前的元素是不是目标元素
     if (att.Value== ""+configKey+"")
     {
     //对目标元素中的第二个属性赋值
     att=nodes[i].Attributes["value"];
     att.Value=configValue;
     break;
     }
     }
     //保存上面的修改
     doc.Save(strFileName);
     }

     public string ReadConfig(string configKey)
     {
         string tempStr = "";
         XmlDocument doc = new XmlDocument();
         doc.Load(strFileName);
         //找出名称为“add”的所有元素
         XmlNodeList nodes = doc.GetElementsByTagName("add");
         for (int i = 0; i < nodes.Count; i++)
         {
             //获得将当前元素的key属性
             XmlAttribute att = nodes[i].Attributes["key"];
             //根据元素的第一个属性来判断当前的元素是不是目标元素
             if (att.Value == "" + configKey + "")
             {
                 //对目标元素中的第二个属性赋值
                 att = nodes[i].Attributes["value"];
                 tempStr= att.Value;
             }
         }
         //保存上面的修改
         return tempStr;
     }

     }

  • 相关阅读:
    索引脚本实例1
    哈尔滨理工大学2016新生赛A题
    拒绝从入门到放弃_《Openstack 设计与实现》必读目录
    生成四位随机数,用于登录验证码制作。
    Python多线程学习(一、线程的使用)
    为什么在 React 16 版本中 render 阶段放弃了使用递归?
    “TensorFlow 开发者出道计划”全攻略,玩转社区看这里!
    【推荐】开源项目ElasticAmbari助力 ElasticSearch、Kibana、ambari服务高效运维管理
    nginx反向代理配置去除前缀
    LNMP架构部署(附:部署Discuz社区论坛Web应用)
  • 原文地址:https://www.cnblogs.com/bestsaler/p/1835506.html
Copyright © 2011-2022 走看看