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.