Codeproject-use the File Attribute of the deleettings element of the. NET configuration file

Source: Internet
Author: User

Original Author: Scott BradleyConnection: http://www.codeproject.com/dotnet/appsettings_fileattribute.asp

Translation: xiaoxin 0574

Introduction Introduction

If you need to share configuration settings among multiple. net assemblies, the practice of maintaining separate config files for each assembly can quickly become tedious. for instance, if you have multiple executables within a directory that all need a 'connectionstring' entry in their config file, the traditional method in. net wocould be for each executable to have its own config file. this can become a burden in an enterprise environment when you need to change the connection string, As you wocould be forced to change each individual config file. fortunately, there is a better approach. this better approach involves using File Attribute of the. NET config file's Appsettings Element. This attribute shoshould contain the relative path to a custom config file, which all other applications can share. The description from msdn on Appsettings File Attribute follows:

If you need . Net ProgramSharing configuration settings between sets makes it very boring to maintain the configuration file for each assembly. For example, if you have multiple executable files in a folder, you must have one 'Connectionstring' Portal, . Net The traditional method is that each executable file has its own configuration file. In an enterprise environment, it becomes a burden, because when you need to change the connection string, you have to change every configuration file. Fortunately, there is a better way. This method includes . Net Configuration File Appsettings Element File Attribute. This attribute can contain relative paths relative to the user configuration file. All programs can share this user configuration file. Msdn Pair Appsettings File The property description is as follows:

specifies a relative path to an external file containing custom application configuration settings. the specified file contains the same kind of settings that are specified in the , , and elements and uses the same key/value pair format as those elements. the path specified is relative to the main configuration file. for a Windows Forms Application, this wocould be the binary folder (such as /bin/debug ), not the location of the application configuration file. for web forms applications, the path is relative to the application root, where the Web. config file is located.

Specify a relative path for an external file that is included in the program configuration. The specified file contains <Add> , <Remove> , And <Clear> The settings in the element are the keys used in the same format as those elements. / Value Pair ( Pair ). The specified path is related to the main configuration file. Pair Windows Forms Program, this may be Binary Folder (for example /Bin/debug ), Rather than the location of the program configuration file. Pair Web Forms For programs, the path is relative to the root program, where Web. config File.

NoteThat the runtime ignores the attribute if the specified file can not be found.

Note: If the specified file cannot be found, the feature is ignored during runtime.

Essential, each executable's config file will contain an entry such:

Essentially, the configuration file of each executable file contains an entry as follows:

<?XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
  <AppsettingsFile= "Settings. config">
  </Appsettings>
</Configuration>

Where"Settings. config"Is a custom config file which looks something like this:

Where"Settings. config"Is a user configuration file, the content is as follows:

< Appsettings >
  < Add Key = "Setting1" Value = "This is setting 1 from settings. config"   />
  < Add Key = "Setting2" Value = "This is setting 2 from settings. config"   />
  < Add Key = "Connectionstring" Value = "Connectstring from settings. confg"   />
</ Appsettings >

When you run your application, you simply useAppsettingsProperty fromSystem. configuration. configurationsettingsTo reference the configuration setting,

When you run your program, you canSystem. configuration. configurationsettingsEasy to useAppsettingsAttribute reference configuration settings:

Dim S As   String   = _
System. configuration. configurationsettings. receivettings ( " Connectionstring " )

In this case, if there is'Connectionstring'Key in the individual config file, that value will be used. Otherwise, the value will be retrieved from the shared"Settings. config"File.

In this case, if there is'Connectionstring'Key in a separate configuration file, the value will be used accordingly. Otherwise, this value will be shared"Settings. config"File.

As indicated above, if you choose, each individual config file can also define the keys which are present in the shared config file. in that case, the individual Config File Settings will take precedence over the shared config file. that cocould be useful in a situation where you want to quickly test a new setting without disrupting the other applications which are using the shared config file.

As mentioned above, if you want to select (another solution), each configuration file can also define keys that exist in the shared configuration file. In that case, individual Configuration File Settings will take precedence over shared configuration files. In this case, it is very useful: You want to quickly test a new setting without interrupting other programs used in the shared configuration file.

Hopefully, this article has shed some light on the sometimes confusing world of. Net configuration files. I hope you find this method useful when dealing with multiple configuration files. Good luck.

We hope this configuration will help you clarify. NetConfiguration files are confusing. I hope you can find this method useful when processing multiple configuration files. Good luck!

Translate this articleArticleI have some questions about the priority mentioned at the end. I did a small experiment by myself. The steps are as follows:

Create a winform program in vs. net2003

Add an app. config item with the following content:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< Appsettings File = "Mysetting. config" >
< Add Key = "Setting1" Value = "My value" > </ Add >
</ Appsettings >
</ Configuration >

Create the mysetting. config file in the debug folder. The content is as follows:

< Appsettings >
  < Add Key = "Setting1" Value = "This is setting 1 from mysetting. config"   />
  < Add Key = "Setting2" Value = "This is setting 2 from mysetting. config"   />
  < Add Key = "Connectionstring" Value = "Connectstring from mysetting. confg"   />
</ Appsettings >

Place a Textbox Control and a button control on the form

Write the following in the button click event:Code:

Textbox1.text = system. configuration. configurationsettings. deleettings ["setting1"];

In the running result, textbox displays: This is setting 1 from mysetting. config.

Why? Didn't the original article say that shared configuration files have a low priority?

Please also know about prawn!

 

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.