Configuration file in C# (detailed explanation)

Configuration file definition:

It is a computer file that configures parameters and initial settings for computer programs, that is, files that perform different configurations for different objects.

Configuration file content:

Comment content: In the sample file, it is represented by # to single-line comment, which is used to explain some necessary content

Configuration item content: The configuration content is actually a record of one key-value pair. The left side is the Key value, and the right side is the Value value. In the middle of the key-value pair, a symbol = is inserted to separate the key value and the value value.

Note: The configuration file stores data in a dictionary format, and a Key value corresponds to a Value value

Configuration file type:

  • The local user profile is created when the user logs on to the computer for the first time. This local user profile is stored on the local hard drive of the computer. Any changes made to the local user profile are only generated on the computer where the change occurred. effect.
  • Roaming user profile, a copy of a local profile is copied and stored on a server share on the network. This file will be downloaded every time the user logs in to any computer on the network, and when the user logs out, any changes to the roaming user profile will be synchronized with the copy of the server.
  • Mandatory user configuration file is a special type of configuration file. Administrators can use it to specify special settings for users. Only the system administrator can modify the mandatory user configuration file. When the user logs off from the system, the user can make changes to the desktop. The changes made will be lost.
  • Temporary configuration files will only appear when the user configuration file cannot be loaded due to an error, such as the json file that appears when the exam system is handing in papers.

Code display:

<?xml version="1.0" encoding="utf-8" ?><!--版本-->
<configuration><!--定义配置类-->
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    <!-- 这里的配置文件用来连接数据库的,在XML配置文件中写入连接数据库的代码,方便打包后其他电脑进行数据连接,
	其他电脑只需要在这里把关键信息改变为他的数据库名称便可以使用,不再需要重新生成等操作 -->
    <!--节点,用来存储应用程序配置信息,如文件路径,xml Web services URL或存储在应用程序的.ini文件中的任何信息-->
	<appSettings>
		
	<!--数据库连接-->
	<add key ="connStr" value="Server=Tom;Database=Seven;User ID=sa;Password=123456"/>
	<!--DB-字符串,通过字符串找到对应的类库-->
	<!--LoginDAL-类库名-->
    <add key ="DB" value="LoginDAL"/>   
	
</appSettings>
 </configuration>

Benefits of configuration files:

If you save some parameters locally, even if you close the software, they will not be lost. They can still be used next time you open it, just like reading files in a stand-alone game.

Precautions:

The configuration file can solve the transplantation and reuse of the code very well, but all things have a degree, the configuration file can not be used too much, and the editing should be case-sensitive.

Xiao Bian's technology is not yet home, and readers are welcome to criticize and correct!

Guess you like

Origin blog.csdn.net/TGB_Tom/article/details/109535679