Net to obtain the app. config parameter/Web. config parameter methods;

Source: Internet
Author: User
Tags connectionstrings

For example:

App. config

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
</Configsections>
<Connectionstrings>
<Add name = "emsconnectionstring" connectionstring = "Data Source =. \ sqlexpress; initial catalog = db_cms; Integrated Security = true"
Providername = "system. Data. sqlclient"/>
</Connectionstrings>
</Configuration>

 

 

 

Web. config

<? XML version = "1.0"?>
<Configuration>
<Appsettings/>
<Connectionstrings>
<Add name = "emsconnectionstring" connectionstring = "Data Source =. \ sqlexpress; initial catalog = db_cms; Integrated Security = true"/>
</Connectionstrings>
<System. Web>
<! --
Set compilation DEBUG = "true" to insert debugging symbols
Compiled pages. However, this
Performance is affected, so this value is only available during development.
Set to true.
-->
<Compilation DEBUG = "true">
</Compilation>
<! --
You can use the <authentication> section to configure ASP. NET
Identifies
Security Authentication mode.
-->
<Authentication mode = "Windows"/>
<! --
If an unprocessed error occurs during request execution,
You can configure the corresponding processing steps in the <mermerrors> section. Specifically,
This section allows developers to configure
HTML error page to be displayed
To replace the error stack trace.

<Customerrors mode = "remoteonly" defaultredirect = "genericerrorpage.htm">
<Error statuscode = "403" Redirect = "noaccess.htm"/>
<Error statuscode = "404" Redirect = "filenotfound.htm"/>
</Customerrors>
-->
</System. Web>
</Configuration>

 

C # Read connectionstring

1. String STR = system. configuration. configurationmanager. connectionstrings ["emsconnectionstring"]. connectionstring. tostring (); or

String Str
= System. configuration. configurationsettings. receivettings ["emsconnectionstring"]. tostring ();

Note: connectionstrings ["emsconnectionstring"] here the brackets contain the value corresponding to name = "emsconnectionstring,

If name = "emsconnectionstring", it should be: connectionstrings ["emsconnectionstring"].

 

2. If you use settings. settings in the Properties column, you can use the following methods to read the settings:

String STR = properties. settings. Default. emsconnectionstring;
This statement is the same as in the preceding example. Strings can be obtained.

 

3. In another way, specify a specific file

Private Static string getconnectionstringsconfig (string connectionname)

{
Execonfigurationfilemap filemap = new execonfigurationfilemap ();
Filemap. execonfigfilename = application. startuppath + @ "\ sss.exe. config ";
Configuration Config = configurationmanager. openmappedexeconfiguration (filemap, configurationuserlevel. None );
Return config. connectionstrings. connectionstrings [connectionname]. connectionstring. tostring ();
}

 

Vbnet reading

1. Dim Str
As string = system. configuration. configurationsettings. receivettings ("emsconnectionstring ")

 

2. My. settings. emsconnectionstring

 

 

C # dynamically read the config file

Public String getconfigvalue (string path, string appkey)

{
Xmldocument xdoc = new xmldocument ();
Try
{
Xdoc. Load (PATH );
// Xdoc. Load (system. Windows. Forms. application. executablepath + ". config ");
Xmlnode xnode;
Xmlelement xelem;
Xnode = xdoc. selectsinglenode ("// appsettings ");
Xelem = (xmlelement) xnode. selectsinglenode ("// Add [@ key = '" + appkey + "']");
If (xelem! = NULL) then
Return xelem. getattribute ("value ");
Else
Return "";
}
Catch (exception)
{
Return "";
}
}

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.