简体   繁体   English

如何使用dotnet在命令行中传递参数?

[英]How to pass arguments in command line using dotnet?

the command dotnet myapp.dll -- [4, 3, 2] throws the exception System.FormatException: Input string was not in a correct format . 命令dotnet myapp.dll -- [4, 3, 2]引发异常System.FormatException: Input string was not in a correct format I do not know the syntax. 我不知道语法。 How should I pass arguments correctly? 我应该如何正确传递参数? I use powershell. 我使用Powershell。

using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(string.Join('-', args));
        }
    }
}

Call it via Powershell 6: 通过Powershell 6调用它:

dotnet .\ConsoleApp3.dll "[1,2,3]"

Output: 输出:

[1,2,3]

In above call your Main will receiv [1,2,3] as single string and you've to parse/split it in your code. 在上面的调用中, Main将以单个字符串的形式接收[1,2,3] ,并且您必须在代码中对其进行解析/拆分。

If you want an array reflected in the string[] -array of Main you can use a PowerShell array: 如果要在Mainstring[] -array中反映一个数组,则可以使用PowerShell数组:

dotnet .\ConsoleApp3.dll @(1,2,3)

Output: 输出:

1-2-3

Here the PowerShell array @(1,2,3) is casted to a string[] -array. 在这里,PowerShell数组@(1,2,3)被强制转换为string[] -array。 Therefore each item of the PowerShell array is injected to the string[] array. 因此,PowerShell数组的每一项都注入到string[]数组中。

Behavior is the same on PowerShell 5.1. 行为在PowerShell 5.1上相同。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将包含逗号的多值参数传递给 dotnet 命令行工具中的属性? - How to pass multi-valued arguments containing commas to a property in dotnet command line tool? 如何使用 bash 脚本将命令行 arguments 输入到 do.net c# 程序? - How to input command line arguments to a dotnet c# program using bash scripts? 如何使用 C# 将参数传递给命令行实用程序? - How to pass arguments to command line utility using c#? 如何将命令行参数传递给 MonoMac 应用程序? - How to pass command line arguments to MonoMac application? 如何将命令行参数传递给 MSI 安装程序 - How to Pass Command Line Arguments to MSI Installer 使用 NUnit 或 XUnit 时如何将参数传递给 dotnet 测试命令 - How to pass parameters to the dotnet test command while using NUnit or XUnit 如何使用别名从命令行运行 dotnet 控制台应用程序 - How to run a dotnet console app from the command line using an alias 如何使用C#语法将DTEXEC实用工具命令行参数传递给process.start? - How do I pass DTEXEC utility command line arguments to process.start using C# syntax? 如何将命令行参数传递给 WinForms 应用程序? - How do I pass command-line arguments to a WinForms application? 如何将参数传递给预构建命令行? - How to pass arguments to the pre-build command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM