How to add a configuration file in VB. NET)

Source: Internet
Author: User

We often use configuration files when working on projects. Why? Is it good for us to use the configuration file? The answer is yes. If we write the strings (such as database connection strings) that may need to be modified in the configuration file, the maintenance personnel can directly modify the configuration file for maintenance in the future, you do not need to modify or recompile your source program.


Since the configuration file can bring us convenience, how should we add and use the configuration file?



First of all, where should I add the configuration file? I think this is what many beginners want to know. Simply put, the configuration file needs to be added to the main assembly (that is, the form application or other applications generated when the project was initially created ):


Compiled and generated in the same directory as the application (.exe file. If no xxx.exe. config file exists in the main directory after compilation, you may not add a correct location.


The second step is how to add a configuration file.

1. Select the main assembly (class library in the red box ).

II,

Method 1: Right-click the class library name (Assembly name) -- add -- new item -- General -- application configuration file (or directly search on the right)

Method 2: Project-add new project-General-application configuration file (or directly search on the right)

3. modify configuration file content

<?xml version="1.0" encoding="utf-8" ?><configuration>    <appSettings>    <add key="DB" value="SQL"/>  </appSettings></configuration>



Finally, how to use the configuration file.


First, reference system. Configuration and add imports at the beginning of the program
System. configuration.

Second, code implementation.

Private Shared ReadOnly dataBase As String = ConfigurationManager.AppSettings("DB")


So far, the configuration file addition and use methods have been introduced, and you can enjoy the benefits it brings to you.

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.