Application configuration file

Source: Internet
Author: User
Some configuration may be required for applications to run on a computer. The CLR configuration file is an XML file. The application can use the configuration file without re-compiling. These configuration files are written in XML, and the element itself is more descriptive than the traditional text-based configuration files .. Net supports a variety of configuration files: machine level, application level, directly related to security. The most common configuration files are application-level configuration files. This article briefly describes how to use the application configuration file. The file name of the application configuration file is the file name of the intellectual file plus. config. If the application name is app.exe, the configuration file should be the app.exe. config file. It should be placed in the/bin folder. The most useful and common part in the application configuration file is the custom configuration part. The configuration file architecture allows custom sections to exist in the configuration file. You can arrange the configuration data in these custom sections .. Net Framework Class Library provides a general configuration processor used by the application, including two classes: system. consiguration. singletagsectionhandler and system. configuration. namevaluesectionhandler. It is best to use an instance to describe the problem. First, create a console project named readcustomdata. Then, in solution manager, right-click the project, add, add a new project, and select the application configuration file. The name is readcustomdata.exe. config. The file content is as follows: <? Xmlversion = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <sectionname = "customsection" type = "system. configuration. singletagsectionhandler "/> </configsections> <customsectionsetting1 =" one "setting2 =" two "setting3 =" three "/> </configuration> then write the following program in the main function: static void main (string [] ARGs) {// todo: Add code here to start the application /// idictionary sampletable = (idictionary) from the configuration file or data) configurationsettings. getconfig ("customsection"); cons Ole. writeline (sampletable ["setting1"]. tostring (); console. writeline (sampletable ["setting2"]. tostring (); console. writeline (sampletable ["setting3"]. tostring (); console. readline ();} execution result: onetwothree Note: You must place the configuration file in the/bin/DEBUG directory. There is another option parsing class system. configuration. namevaluesectionhandler. To use this in the configuration file, the configuration file and program should be written as follows: <? Xmlversion = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <sectionname = "mnmnamedsection" type = "system. configuration. namevaluesectionhandler "/> </configsections> <mnmnamedsection> <addkey =" setting1 "value =" one "/> <addkey =" setting2 "value =" two "/> <addkey = "setting3" value = "three"/> </customnamedsection> </configuration> program: static void main (string [] ARGs) {// todo: Add code here to start the application /// obtain data from the configuration file namevaluecollection sampletable = (namevaluecollection) configurationsettings. getconfig ("mnmnamedsection"); console. writeline (sampletable ["setting1"]); console. writeline (sampletable ["setting2"]); console. writeline (sampletable ["setting3"]); console. readline ();}

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.