欢迎光临
个人技术文档整理

.Net 要传递到可执行文件的命令参数 AddCommandLine

NuGit安装

Install-Package  Microsoft.Extensions.Configuration.CommandLine
Install-Package  ​​​​​​​Microsoft.Extensions.Configuration

 

命令行支持几种命令格式

  1. 无前缀模式: key=value 模式  如: type=B id=123
  2. 双中横线模式: --key = value 或 --key vlaue 如:--type=B id=123 或则  --type B --id 123
  3. 正斜杠模式: /key=value 或/key value 如:/type=B id=123 或则  /type B /id 123

注:如果--key = value 这种等号模式,那么就不能使用--key vlaue 这种中间空格二点模式。

 

实例

   [STAThread]
        static void Main(string[] args)
        {

            ApplicationConfiguration.Initialize(); 

            #region 获取 命令行 参数
            //Microsoft.Extensions.Configuration.CommandLine
            //Microsoft.Extensions.Configuration
            var builder = new ConfigurationBuilder().AddCommandLine(args);
            var configuration = builder.Build();
            var type = configuration["type"];
            var id = configuration["id"];
            #endregion

            var str = type + "#" + id;

            if (type == "B")
            {
                Application.Run(new BForm());
            }
            else
            {
                Application.Run(new Form1());
            }



        }

使用方法

  1. 调试->设置命令行参数
  2. 第二种传参
    找到项目生成的目录【bin\Debug\net6.0-windows】->输入CMD命令->xxx.exe --type=B

 

 

赞(1)