本文由 发布,转载请注明出处,如有问题请联系我们! 发布时间: 2021-06-01重新整理 .net core 实践篇————配置系统——军令(命令行)[六]

加载中

分类整理 .net core 实践活动篇————配备系统软件——军令(cmd)[六]

序言

前文早已基本上写了一下环境变量系统软件的一些基本概念。文中介绍一下cmd导进配备系统软件。

文章正文

要应用得话,引进Microsoft.extensions.Configuration.commandLine 包。

编码:

IConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddCommandLine(args);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.ReadLine();

载入检测主要参数:

結果:

另一个难题,便是cmd适用几类指令文件格式。

  1. 无作为前缀方式 key=value 方式

  2. 双中水平线方式 --key = value 或 --key vlaue

  3. 正斜线方式 /key=value 或/key value

注:假如--key = value 这类等于号方式,那麼就不可以应用--key vlaue 这类正中间空格符二点方式。
配备:

IConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddCommandLine(args);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.WriteLine($"CommandLineKey2:{configurationRoot["CommandLineKey2"]}");
Console.WriteLine($"CommandLineKey3:{configurationRoot["CommandLineKey3"]}");
Console.ReadLine();

結果:

这儿实际上新项目特性在lauchSettings.json 中,后边我不截屏,立即放这一运行配备。

指令更换方式:

{
  "profiles": {
    "ConfigureDemo": {
      "commandName": "Project",
      "commandLineArgs": "CommandLineKey1=value1 /CommandLineKey2=value2 --CommandLineKey3=value3 -k1=value4"
    }
  }
}

编码:

IConfigurationBuilder builder = new ConfigurationBuilder();
var mappers = new Dictionary<string, string>
{
	{"-k1","CommandLineKey1" }
};
builder.AddCommandLine(args, mappers);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.WriteLine($"CommandLineKey2:{configurationRoot["CommandLineKey2"]}");
Console.WriteLine($"CommandLineKey3:{configurationRoot["CommandLineKey3"]}");
Console.ReadLine();

这里边便是以-k1的值更换了CommandLineKey1的值。

以上的-k1也不是随意取名的,要用-开始才能够更换。

那麼这类有什么作用呢?

这类能够减少取名。

{
  "profiles": {
    "ConfigureDemo": {
      "commandName": "Project",
      "commandLineArgs": "-k1=value4"
    }
  }
}

编码:

IConfigurationBuilder builder = new ConfigurationBuilder();
var mappers = new Dictionary<string, string>
{
	{"-k1","CommandLineKey1" }
};
builder.AddCommandLine(args, mappers);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.ReadLine();

結果:

下一节配备系统之变色龙(自然环境配备)

以上为本人梳理,若有不正确望请强调,感谢。

评论(0条)

刀客源码 游客评论