Read and Write configuration file information on the Web

Source: Internet
Author: User

 

Web configuration file read/write and client applicationProgramThe configuration file reads and writes are almost identical. However, Web. config has multiple inheritance issues.
If the sub-website directory is not configured, it will inherit from the parent directory. And so on, this is a problem of multi-layer inheritance.

This is also the configuration class of the previous blog. Let's take a look at the read and write operations on the web.
Using system. Web. configuration;

Write operation:
Unmount the original section and Add a new section for write operations.
Configdataclass configdata = new configdataclass ();
Configdata. ID = 100;
Configdata. Name = "who am I? ";
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
Config. Sections. Remove ("settingsdata ");
Config. Sections. Add ("settingsdata", configdata );
Config. Save ();

Modification operations based on the original section
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
Configdataclass configdata = config. getsection ("settingsdata") as configdataclass;
Configdata. ID = 1003;
Configdata. Name = "I ";
Config. Save ();

Read operation:
Configdataclass configdata = configurationmanager. getsection ("settingsdata") as configdataclass;
If (configdata = NULL) return string. empty;
Return configdata. tostring ();

We can see from the above
Besides
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
This lineCodeUnlike the referenced namespace using system. Web. configuration;, the web program is identical with the client application.

Whether it is a Web application or a client application, sometimes a configuration file is not enough. We may need multiple configuration files, next we will look at how to read and write other configuration files as the default configuration file of the system.
Note that the system. configuration. configuration class is used at this time. You can use this class for both web programs and client applications.

Update another configuration file
Configurationfilemap filemap = new configurationfilemap ();
Filemap. machineconfigfilename = @ "D: \ my2005codes \ webtestcode \ testwebsite \ AA. config ";
System. configuration. Configuration Config = configurationmanager. openmappedmachineconfiguration (filemap );

Configdataclass configdata = new configdataclass ();
Configdata. ID = 303;
Configdata. Name = "csdn? ";

Config. Sections. Remove ("settingsdata ");
Config. Sections. Add ("settingsdata", configdata );
Config. Save ();

Before writing, the configuration file does not have the preceding configuration section, but must have the following lines of information. Otherwise, no root node is reported.
<? XML version = "1.0"?>
<Configuration>
</Configuration>

After the write operation is completed, the AA. config file becomes

<? XML version = "1.0"?>
<Configuration>
<Configsections>
<Section name = "settingsdata" type = "configdataclass, app_code.yltp08ba, version = 0.0.0.0, culture = neutral, publickeytoken = NULL"/>
</Configsections>
<Settingsdata id = "303" name = "csdn? "/>
</Configuration>

Read another specified configuration file

Configurationfilemap filemap = new configurationfilemap ();
Filemap. machineconfigfilename = @ "D: \ my2005codes \ webtestcode \ testwebsite \ AA. config ";
System. configuration. Configuration Config = configurationmanager. openmappedmachineconfiguration (filemap );

Configdataclass configdata = config. getsection ("settingsdata") as configdataclass;
If (configdata = NULL) return string. empty;
Return configdata. tostring ();

For client applications, you can use system. configuration. configurationmanager. openmappedexeconfiguration to read and write the default configuration files of multiple client applications.

Of course, these functions can be fully used for class serialization and deserialization. But since this function is available, Do you not need it?

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.