C # exe. config application configuration file

Source: Internet
Author: User

Similar to remotingconfiguration. Configure ("syncasync.exe. config ");
Applicationname.exe. config you can use the system. Configuration namespace from
Get the application in the XML. config fileProgramConfiguration information.
ExampleCode:
<Deleetask>
<! -- Add key = "constring" value = "Server = zsl; uid = sa; Pwd =; database = OA" -->
<Add key = "constring" value = "provider = sqloledb; Data Source = zsl; initial catalog = OA; user id = sa; Password =;"/>
</Appsettings>
Configurationsettings. deleettings ["constring"]. tostring ()

It is only verified at the beginning of the program, and the file contains a script file. How can the CDATA file be solved?

How to Use the custom configuration section in the web. config file --

[Reprint] How to Use the custom configuration section in the web. config file

A simple example of how to use the custom configuration section in the web. config file

The program name used for demonstration is MyApp, and namespace is also MyApp

1. Edit the Web. config file

Add the following content to declare a Section

<Configsections>
<Section name = "appconfig" type = "MyApp. appconfig, MyApp"/>
</Configsections>

Declared a section called appconfig

2. Edit the Web. config file

Add the following content to a section

<Appconfig>
<Add key = "connectionstring" value = "This Is A connectionstring"/>
<Add key = "usercount" value = "199"/>
</Appconfig>
This section contains two keys

3. Derive a class from iconfigurationsectionhandler, appconfig

The code for implementing the create method is as follows:

Public class appconfig: iconfigurationsectionhandler
{
Static string m_connectionstring = string. empty;
Static int32 m_usercount = 0;
Public static string connectionstring
{
Get
{
Return m_connectionstring;
}
}
Public static int32 usercount
{
Get
{
Return m_usercount;
}
}

Static string readsetting (namevaluecollection NVC, string key, string defaultvalue)
{
String thevalue = NVC [Key];
If (thevalue = string. Empty)
Return defaultvalue;

Return thevalue;
}

Public object create (Object parent, object configcontext, xmlnode Section)
{
Namevaluecollection settings;

Try
{
Namevaluesectionhandler basehandler = new namevaluesectionhandler ();
Settings = (namevaluecollection) basehandler. Create (parent, configcontext, Section );
}
Catch
{
Settings = NULL;
}

If (settings! = NULL)
{
M_connectionstring = appconfig. readsetting (settings, "connectionstring", String. Empty );
M_usercount = convert. toint32 (appconfig. readsetting (settings, "usercount", "0 "));
}

Return settings;
}
}

We map all the configurations to the corresponding static member variables and write them as read-only attributes.

Similar to appconfig. connectionstring, you can access the project in the configuration file.

4. Finally, we have to do one thing.

Add the following code to application_start in global. asax. CS:

System. configuration. configurationsettings. getconfig ("appconfig ");

After the program starts, it will read the value in the section appconfig. The system will call your own iconfigurationsectionhandler interface to read the 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.