zoukankan      html  css  js  c++  java
  • .net 5.0

     引用NuGet

     Microsoft.Extensions.Configuration

     Microsoft.Extensions.Configuration.FileExtensions

     Microsoft.Extensions.Configuration.Json

     在控制台应用项目下,创建appsettings.json
    {
      "ConnectionStrings": {
        "dbName": "server=localhost;database=hr;PORT=3306;uid=root;pwd=123456;charset=gb2312"
      },
      "AppSettings": {
        "Url": "12"
      }
    }
    

     特别注意:

    创建调用类及调用方式
    using Microsoft.Extensions.Configuration;
    using System;
    using System.IO;
    
    namespace CustomAttributes.Model
    {
        public class Appsettings
        {
            private IConfigurationRoot Configuration;
    
            private Appsettings()
            {
                var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
                Configuration = builder.Build();
            }
    
            private static readonly Lazy<Appsettings> InstanceLock = new Lazy<Appsettings>(() => new Appsettings()
            {
    
            });
    
            public static Appsettings Instance
            {
                get
                {
                    return InstanceLock.Value;
                }
            }
    
            public string GetByKey(string key)
            {
                return Configuration.GetSection(key).Value;
            }
    
            public string GetConnectionString()
            {
                return Configuration.GetConnectionString("dbName");
            }
        }
    }
    

      

    using CustomAttributes.Model;
    using System;
    
    namespace CustomAttributesApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(Appsettings.Instance.GetByKey("AppSettings:Url"));
                Console.WriteLine(Appsettings.Instance.GetConnectionString());
            }
        }
    }
    

      

  • 相关阅读:
    【SR】MAP
    【SR】Example-based
    【SR】论文资源相关
    【SR】正则化超分辨率复原
    词汇累记
    单模光纤与多模光纤的实际使用
    光纤中的光波长
    数码相机常用CCD/CMOS尺寸对比
    尼康D90多点对焦
    Python3.x:日期库dateutil简介
  • 原文地址:https://www.cnblogs.com/gygtech/p/14762431.html
Copyright © 2011-2022 走看看