Passing Command-Line Arguments in C#

Passing command-line arguments in C#

I just ran a check and verified the problem. It surprised me, but it is the last \ in the first argument.

"C:\Program Files\Application name\" <== remove the last '\'

This needs more explanation, does anybody have an idea? I'm inclined to call it a bug.


Part 2, I ran a few more tests and

"X:\\aa aa\\" "X:\\aa aa\" next

becomes

X:\\aa aa\
X:\\aa aa" next

A little Google action gives some insight from a blog by Jon Galloway, the basic rules are:

  • the backslash is the escape character
  • always escape quotes
  • only escape backslashes when they precede a quote.

Passing command line arguments in C# Visual Studio by code

To get user input in "arg" format, you use Console.ReadLine():

string[] userArgs = Console.ReadLine().Split(' ');

The Console.ReadLine gets the entire input string, then to get the array we call .Split(). I split on spaces just like standard command line. Other delimiters are also possible, just pass in a different character to the Split function.

Of course, you could stick to using normal command line args and have the user enter them as part of the program call (not normally done on Windows since few people use command prompt, but it would work).

MSDN for Split in case you are interested!

c# Command Line arguments

Is this, what you want?

class MainClass
{
static void Main(string[] args)
{
// Test if input arguments were supplied:
var switchvalue = int.Parse(args[0]);
if (switchvalue == 1)
{
int port = int.Parse(args[1]);
server = new TcpListener(IPAddress.Any, port);
//Rest of the program
}
if (switchvalue == 2)
{
int port = int.Parse(args[1]);
server = new TcpListener(IPAddress.Any, port);
//Rest of the program
}
if (switchvalue == 3)
{
int port = int.Parse(args[1]);
server = new TcpListener(IPAddress.Any, port);
//Rest of the program
}
}
}

How to set C# application to accept Command Line Parameters?

All command line parameters are passed to your application through string[] args parameter.
The code below shows an example:

using System.Linq;

namespace MyApp
{
class Program
{
static void Main(string[] args)
{
if (args.Contains("/REPORT1"))
{
/* Do something */
}
else if (args.Contains("/REPORT2"))
{
/* Do something */
}
}
}
}

Then, in command prompt just use:

C:\MyApp\MyApp.exe /REPORT1

Console app arguments, how arguments are passed to Main method

The Main method is the Entry point of your application. If you checkout via ildasm then

.method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint

This is what helps in calling the method

The arguments are passed as say C:\AppName arg1 arg2 arg3

Best way to pass command line arguments between processes

There's so many ways to do this, IMHO.

A quick and dirty way would be to anonymous pipes. Just before you quit the second instance, you could make a call over the pipes, passing the command line args (maybe do a string.Join(new []{','}, args) to concatenate them first).

There's so many other ways to do inter-process communications though.

A very simplistic example of anonymous pipes usage is give on MSDN:
https://msdn.microsoft.com/en-us/library/bb546102(v=vs.110).aspx

HTH.

How do I pass command-line arguments to a WinForms application?

static void Main(string[] args)
{
// For the sake of this example, we're just printing the arguments to the console.
for (int i = 0; i < args.Length; i++) {
Console.WriteLine("args[{0}] == {1}", i, args[i]);
}
}

The arguments will then be stored in the args string array:

$ AppB.exe firstArg secondArg thirdArg
args[0] == firstArg
args[1] == secondArg
args[2] == thirdArg


Related Topics



Leave a reply



Submit