Location>code7788 >text

Read configuration files

Popularity:934 ℃/2025-03-06 23:28:54

Reference video (Yang Zhongke):

Configuration System 1-Beginner:
/video/BV1pK41137He?vd_source=b7200d0eaee914e9c128dcabce5df118&p=40&spm_id_from=333.
Configure System 2-Options Read:
/video/BV1pK41137He?vd_source=b7200d0eaee914e9c128dcabce5df118&spm_id_from=333.&p=41
Configuration System 3-Other Configuration Providers:
/video/BV1pK41137He?vd_source=b7200d0eaee914e9c128dcabce5df118&spm_id_from=333.&p=42
Configuration System 4-Develop your own configuration provider:
/video/BV1pK41137He?vd_source=b7200d0eaee914e9c128dcabce5df118&spm_id_from=333.&p=43
Configuration System 5-Development Database Configuration Provider
/video/BV1pK41137He?vd_source=b7200d0eaee914e9c128dcabce5df118&spm_id_from=333.&p=44
Configuration System 6 - Priority of Multi-Configuration Source
/video/BV1pK41137He?vd_source=b7200d0eaee914e9c128dcabce5df118&spm_id_from=333.&p=45

References:

/cnblogs_com/blogs/838411/galleries/2448086/t_250306151340_Pasted image

Actual use

Read

Create a new console program

Add test configuration files and set properties

/cnblogs_com/blogs/838411/galleries/2448086/t_250306151340_Pasted image

Installation and

Write the following code (I used the top-level statement here):

// See /new-console-template for more information
using ;

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
("", optional: false, reloadOnChange: false);
IConfigurationRoot configurationRoot = ();
string name = configurationRoot["name"];
($"name = {name}");
string proxyAddress = ("proxy:address").Value;
($"address={proxyAddress}");

Run the code to view the results:

/cnblogs_com/blogs/838411/galleries/2448086/t_250306151340_Pasted image

Brief analysis of the code:

("", optional: false, reloadOnChange: false):

  • Added a configuration file named "" to be parsed.
  • optional: indicates whether this file is optional. False means that an error will be reported when the configuration file does not exist.
  • reloadOnChange: indicates whether to reload the configuration if the file is modified
Test the above properties:
1. Change to

/cnblogs_com/blogs/838411/galleries/2448086/t_250306151340_Pasted image

2. Change to && optional: true

/cnblogs_com/blogs/838411/galleries/2448086/t_250306151340_Pasted image

3、reloadOnChange: true

You need to modify the code to continuously print it on the console, and then modify the configuration file. Please refer to the following code for modifying it;

// See /new-console-template for more information
using ;


await (async () =>
{
    while (true)
    {
        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        ("", optional: false, reloadOnChange: true);
        IConfigurationRoot configurationRoot = ();
        string name = configurationRoot["name"];
        ($"name = {name}");
        string proxyAddress = ("proxy:address").Value;
        ($"address={proxyAddress}");
        await (2000);
    }
});

/cnblogs_com/blogs/838411/galleries/2448086/t_250306151340_Pasted image
Modify the configuration file
/cnblogs_com/blogs/838411/galleries/2448086/t_250306151340_Pasted image
!!!Not updated

I went to watch the author's video and found:

/cnblogs_com/blogs/838411/galleries/2448086/t_250306151340_Pasted image