C#对Web.config的读写操作

来源:互联网 发布:tv霸网络电视在线直播 编辑:程序博客网 时间:2024/05/14 18:07

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HeNWry.Model;
using HeNWry.BLL;
using System.Configuration;
using System.Xml;

 public void read()
  {
   
   string filename=Server.MapPath("../")+@"Web.config";
   XmlDocument  xmldoc= new XmlDocument();
   xmldoc.Load(filename);
 
   XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
   foreach(XmlElement element in topM)
   {
    if(element.Name=="appSettings")
    {
     XmlNodeList node=element.ChildNodes;
     if ( node.Count > 0 )
     {
      foreach(XmlElement el in node)
      {
       switch(el.Attributes["key"].Value)
       {
        case "day":
         this.txtDAY.Text = el.Attributes["value"].Value;
         break;
        case "hour":
         this.txtHOUR.Text = el.Attributes["value"].Value;
         break;
        case "min":
         this.txtMiM.Text = el.Attributes["value"].Value;
         break;
       }
       
      }
      
     }
    }
   }
  }

  public void write()
  {
   string filename=Server.MapPath("../")+@"Web.config";
   XmlDocument  xmldoc= new XmlDocument();
   xmldoc.Load(filename);

   XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
   foreach(XmlElement element in topM)
   {
    if(element.Name=="appSettings")
    {
     XmlNodeList node=element.ChildNodes;
     if ( node.Count > 0 )
     {
      foreach(XmlElement el in node)
      {
       switch(el.Attributes["key"].Value)
       {
        case "day":
         el.Attributes["value"].Value = this.txtDAY.Text;
         break;
        case "hour":
         el.Attributes["value"].Value =this.txtHOUR.Text;
         break;
        case "min":
         el.Attributes["value"].Value =this.txtMiM.Text;
         break;
       }
      }
     }
    }
   }
   xmldoc.Save(filename);
  }
  public void resert()
  {
   string filename=Server.MapPath("../")+@"Web.config";
   XmlDocument  xmldoc= new XmlDocument();
   xmldoc.Load(filename);

   XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
   foreach(XmlElement element in topM)
   {
    if(element.Name=="appSettings")
    {
     XmlNodeList node=element.ChildNodes;
     if ( node.Count > 0 )
     {
      foreach(XmlElement el in node)
      {
       switch(el.Attributes["key"].Value)
       {
        case "day":
         el.Attributes["value"].Value = "0";
         break;
        case "hour":
         el.Attributes["value"].Value ="6";
         break;
        case "min":
         el.Attributes["value"].Value = "10";
         break;
       }
      }
     }
    }
   }
   xmldoc.Save(filename);
  }

  public static string Gettime()
  {
   
   string day="";
   string hour="";
   string min="";
   Page dd = new Page();
   
   string filename=  dd.Server.MapPath("/")+@"Web.config";
   XmlDocument  xmldoc= new XmlDocument();
   xmldoc.Load(filename);
 
   XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
   foreach(XmlElement element in topM)
   {
    if(element.Name=="appSettings")
    {
     XmlNodeList node=element.ChildNodes;
     if ( node.Count > 0 )
     {
      foreach(XmlElement el in node)
      {
       switch(el.Attributes["key"].Value)
       {
        case "day":
         day= el.Attributes["value"].Value;
         break;
        case "hour":
         hour = el.Attributes["value"].Value;
         break;
        case "min":
         min = el.Attributes["value"].Value;
         break;
       }
       
      }
      
     }
    }
   }


   stringnowtime=DateTime.Now.AddDays(int.Parse("-"+day)).AddHours(int.Parse("-"+hour)).AddMinutes(int.Parse("-"+min)).ToString();
   return nowtime;
  }

//

web.config

<appSettings>
          <add key="day" value="0" />
    <add key="hour" value="6" />
    <add key="min" value="10" />
  </appSettings>