1.9 配置框架:使用命令行配置提供程序接收命令行参数.pdf

    直接在 launchSettings.json 中编辑命名行参数:

    1. {
    2. "profiles": {
    3. "ConfigurationCommandLineDemo": {
    4. "commandName": "Project",
    5. "commandLineArgs": "CommandLineKey1=value1 --CommandLineKey2=value2 /CommandLineKey3=value3 -k1=k3"
    6. }
    7. }
    8. }
    1. static void Main(string[] args)
    2. {
    3. var builder = new ConfigurationBuilder();
    4. //builder.AddCommandLine(args);
    5. #region 命令替换
    6. var mapper = new Dictionary<string, string> { { "-k1", "CommandLineKey1" } };
    7. builder.AddCommandLine(args, mapper);
    8. #endregion
    9. var configurationRoot = builder.Build();
    10. Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
    11. Console.WriteLine($"CommandLineKey2:{configurationRoot["CommandLineKey2"]}");
    12. Console.ReadKey();
    13. }