• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#手动读写appconfig的源码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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;
 }
 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
學習.Net(c#)打印--多頁打印发布时间:2022-07-10
下一篇:
浅谈委托和事件之初识C#委托发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap