Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I do not know the syntax. How should I pass arguments correctly? I use powershell.

like image 909
Сергей Avatar asked May 13 '19 08:05

Сергей


People also ask

How do I pass a command line argument in Visual Studio?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.

How do I run command line arguments?

A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.

How do I execute a Dot Net command?

When you publish your app as an FDD, a <PROJECT-NAME>. dll file is created in the ./bin/<BUILD-CONFIGURATION>/<TFM>/publish/ folder. To run your app, navigate to the output folder and use the dotnet <PROJECT-NAME>. dll command.


1 Answers

using System;

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

Call it via Powershell 6:

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

Output:

[1,2,3]

In the above call, your Main method will receive [1,2,3] as a single string and you have to parse/split it in your code.

If you want an array reflected in the string[] array of Main you can use a PowerShell array:

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

Output:

1-2-3

Here the PowerShell array @(1,2,3) is casted to a string[]-array. Therefore each item of the PowerShell array is injected to the string[] array.

Behavior is the same on PowerShell 5.1.

like image 191
Moerwald Avatar answered Oct 14 '22 06:10

Moerwald