C # read/write operations on Web. config

Source: Internet
Author: User

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 = "etettings ")
{
Xmlnodelist node = element. childnodes;
If (node. Count> 0)
{
Foreach (xmlelement El in node)
{
Switch (El. attributes ["key"]. value)
{
Case "day ":
This.txt day. Text = El. attributes ["value"]. value;
Break;
Case "Hour ":
This.txt hour. Text = El. attributes ["value"]. value;
Break;
Case "min ":
This.txt Mim. 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 = "etettings ")
{
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.txt day. text;
Break;
Case "Hour ":
El. attributes ["value"]. Value implements this.txt hour. text;
Break;
Case "min ":
El. attributes ["value"]. Value implements this.txt Mim. 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 = "etettings ")
{
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 = "etettings ")
{
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;
}

}

}
}
}

String
Nowtime = datetime. now. adddays (Int. parse ("-" + day )). addhours (Int. parse ("-" + hour )). addminutes (Int. parse ("-" + min )). tostring ();
Return nowtime;
}

//

Web. config

<Deleetask>
<Add key = "day" value = "0"/>
<Add key = "Hour" value = "6"/>
<Add key = "min" value = "10"/>
</Appsettings>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.