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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With