zoukankan      html  css  js  c++  java
  • .net Core命令行,Json配置

    创建.netCore控制台

    NuGet :Microsoft.AspNetCore.All

         static void Main(string[] args)
            {

           var builder = new ConfigurationBuilder() .AddCommandLine(args);//扩展函数 var configuration = builder.Build();//绑定到configuration Console.WriteLine($"name:{configuration["name"]}"); Console.WriteLine($"Age:{configuration["Age"]}"); Console.ReadLine(); }

    直接运行没有结果,然后在应用程序参数

    输出结果 

    另一种方式,给与默认值

          static void Main(string[] args)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>()
                {
                    { "name","Zhanglong1"},
                    { "Age","Age=22"}
                };
    
                var builder = new ConfigurationBuilder()
                    .AddInMemoryCollection(dic)//默认值
                    .AddCommandLine(args);
                var configuration = builder.Build();
                Console.WriteLine($"name:{configuration["name"]}");
                Console.WriteLine($"Age:{configuration["Age"]}");
                Console.ReadLine();
            }

     Json文件配置

    控制台中添加.json文件

    然后Main方法

     static void Main(string[] args)
            {
                var builder = new ConfigurationBuilder()
                    .AddJsonFile("Class.json",false,true);//方法重载,1文件夹2当不存在的时候是否抛异常3当文件改变时是否重新加载配置
                var configuration = builder.Build();
                Console.WriteLine($"ClassNo:{configuration["ClassNo"]}");
                Console.WriteLine($"ClassDesc:{configuration["ClassDesc"]}");
                Console.WriteLine("Students");
                Console.Write($"Name={configuration["Students:0:name"]}");
                Console.WriteLine($"Name={configuration["Students:0:Age"]}");
                Console.Write($"Name={configuration["Students:1:name"]}");
                Console.WriteLine($"Name={configuration["Students:1:Age"]}");
                Console.Write($"Name={configuration["Students:2:name"]}");
                Console.WriteLine($"Name={configuration["Students:2:Age"]}");
                Console.ReadLine();
            }

    输出结果:

    jsonpatch.com https://dotnetcoretutorials.com/?s=jsonpatch&submit=Search

  • 相关阅读:
    Bzoj1305 [CQOI2009]dance跳舞
    Bzoj1269 [AHOI2006]文本编辑器editor
    Bzoj2957 楼房重建
    POJ1704 Georgia and Bob
    UVa11427 Expect the Expected
    POJ2096 Collecting Bugs
    Bzoj3041 水叮当的舞步
    Bzoj3894 文理分科
    Bzoj1426 收集邮票
    Bzoj1076 [SCOI2008]奖励关
  • 原文地址:https://www.cnblogs.com/AnkerZhang/p/8358402.html
Copyright © 2011-2022 走看看