Yes, this is what string[] args
is for in Program.cs
.
The easiest way to ensure you have everything you need is to create your application from the Console Application template in Visual Studio.
To do this:
- Go to File -> New Project...
- Select
Console Application
from the list of available templates
- Name your Solution
- Hit OK to finish creating the Project
This will ensure you have all the references needed and that the Program.cs
class is setup properly.
Here is a psuedo example of what you can do in the Main
method of your program in order to accept parameters at the command line:
static void Main(string[] args)
{
if (args.Length <= 0) //Checking the Length helps avoid NullReferenceException at args[0]
{
//Default behavior of your program without parameters
}
else
{
if (args[0] == "/?")
{
//Show Help Manual
}
if (args[0] == "/d")
{
//Do something else
}
//etc
}
}
Then at the command prompt the user can type:
yourProgamName.exe /?
yourProgramName.exe /d
As mentioned in an answer that was removed, there is a great library for handling complex Command Line options Here:
NDesk Options