C # app. config

Source: Internet
Author: User

After entering the company for more than a year, I have added many parameters to the configuration file, but I never thought about how these parameters are read. Today I have read the parameters and gained a lot. Suppose there is app. config as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "family" type = "firstrowtest. configuration. settings, firstrowtest"/>
</Configsections>
<Family>
<Familymember>
<Member status = "father"
Birth = "1954"
Name = "AA"
Holobby = "reading books">
</Member>
<Member status = "mother"
Birth = "1958"
Name = "BB"
Holobby = "chatting">
</Member>
<Member status = "elder brother"
Birth = "1982"
Name = "cc"
Holobby = "money">
</Member>
<Member status = "younger brother"
Birth = "1986"
Name = "DD"
Holobby = "computer">
</Member>
</Familymember>
</Family>
<Deleetask>
<Add key = "Address" value = "Maling"/>
</Appsettings>
</Configuration>

Structure:

Public class Param: system. configuration. configurationelement
{
[System. configuration. configurationproperty ("status")]
Public String status
{
Get
{
Return this ["status"] as string;
}
Set
{
This ["status"] = value;
}
}
[System. configuration. configurationproperty ("birth")]
Public String birth
{
Get
{
Return this ["birth"] as string;
}
Set
{
This ["birth"] = value;
}
}
[System. configuration. configurationproperty ("name")]
Public string name
{
Get
{
Return this ["name"] as string;
}
Set
{
This ["name"] = value;
}
}
[System. configuration. configurationproperty ("holobby")]
Public String holobby
{
Get
{
Return this ["holobby"] as string;
}
Set
{
This ["holobby"] = value;
}
}
}

Public class Params: system. configuration. configurationelementcollection
{
Protected override system. configuration. configurationelement createnewelement ()
{
Return new param ();
}
Protected override object getelementkey (system. configuration. configurationelement element)
{
Param = element as Param;
Return Param. status;
}
Protected override string elementname
{
Get
{
Return "member ";
}
}
Public override system. configuration. configurationelementcollectiontype collectiontype
{
Get
{
Return System. configuration. configurationelementcollectiontype. basicmap;
}
}

}

Public class settings: system. configuration. configurationsection
{
[System. configuration. configurationproperty ("familymember")]
Public Params familymember
{
Get
{
Return this ["familymember"] As Params;
}
Set
{
This ["familymember"] = value;
}
}
}

Call:

Public frmmain ()
{
Initializecomponent ();
}
Private void frmmain_load (Object sender, eventargs E)
{
System. configuration. Configuration CFG = system. configuration. configurationmanager. openexeconfiguration (system. configuration. configurationuserlevel. None );
// Settings setting = cfg. getsection ("family") as settings;
Settings setting = system. configuration. configurationmanager. getsection ("family") as settings;
Param first = NULL;
If (null! = Setting)
{
Foreach (Param P in setting. familymember)
{
If (null = first)
{
First = P;
}
This. richtextbox1.appendtext (string. Format ("status: {0} | birth: {1} | Name: {2} | holobby: {3} \ r \ n ",

P. Status, P. Birth, P. Name, P. holobby ));
}
}
// Cfg. Save ();
System. configuration. configurationmanager. refreshsection ("family ");
}
}

If you want to save it, there is a slight change in processing:

Public partial class frmmain: Form
{
Public frmmain ()
{
Initializecomponent ();
}
Private void frmmain_load (Object sender, eventargs E)
{
System. configuration. Configuration CFG = system. configuration. configurationmanager. openexeconfiguration (system. configuration. configurationuserlevel. None );
Settings setting = cfg. getsection ("family") as settings;
// Settings setting = system. configuration. configurationmanager. getsection ("family") as settings;
Param first = NULL;
If (null! = Setting)
{
Foreach (Param P in setting. familymember)
{
If (null = first)
{
First = P;
}
This. richtextbox1.appendtext (string. Format ("status: {0} | birth: {1} | Name: {2} | holobby: {3} \ r \ n ",

P. Status, P. Birth, P. Name, P. holobby ));
}
}

First. Name = "I dont know ";
Cfg. Save ();
// System. configuration. configurationmanager. refreshsection ("family ");
}

Save the result:

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "family" type = "firstrowtest. configuration. settings, firstrowtest"/>
</Configsections>
<Family>
<Familymember>
<Member status = "father" Birth = "1954" name = "I dont know" holobby = "reading books"/>
<Member status = "mother" Birth = "1958" name = "BB" Hober = "chatting"/>
<Member status = "elder brother" Birth = "1982" name = "cc" holobby = "money"/>
<Member status = "younger brother" Birth = "1986" name = "DD" holobby = "computer"/>
</Familymember>
</Family>
<Deleetask>
<Add key = "Address" value = "Maling"/>
</Appsettings>
</Configuration>

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.