How to get command line arguments for windows application in c#
I am new in coding..... I mean i don't know any thing about software development.... I am right now learning C#...... I was asked a question by my lecturer that How to get command line arguments for windows application in c#...???? Does any one here know how to get it..???? Please Help....!
Re: How to get command line arguments for windows application in c#
A useful technique for its applications is what it allows them to analyze the line of commandos. This can give a great amount of additional functions to its request, for example, to pass the name of a file abrir it in the line of commandos.
The majority of the examples that will find in line will show something to him like this:
Code:
static void Main(string[] args)
{
foreach(string arg in args)
{
Console.WriteLine(arg);
}
Console.ReadLine();
}
That is all good good and, unless they do not work for our application of Forms Windows without changing the type of project for the console, etc
This is completely unnecessary, since simply it can do this:
Code:
string[] args = Environment.GetCommandLineArgs();
foreach(string arg in args){
// Your Things
}
And it can use this in any place of its application, not only main () like in an application of console is limited its use in the method.
Re: How to get command line arguments for windows application in c#
The following example shows how to use command-line arguments in a console application. The program takes one argument at run time, converts the argument to an integer, and calculates the factorial of the number. If no arguments are supplied, the program issues a message that explains the correct usage of the program.
Code:
public class Functions
{
public static long Factorial(int n)
{
if ((n <= 0) || (n > 256))
{
return -1;
}
long tempResult = 1;
for (int i = 1; i <= n; i++)
{
tempResult *= i;
}
return tempResult;
}
}
class MainClass
{
static int Main(string[] args)
{
if (args.Length == 0)
{
System.Console.WriteLine("Please enter a numeric argument.");
System.Console.WriteLine("Usage: Factorial <num>");
return 1;
}
int num;
bool test = int.TryParse(args[0], out num);
if(test == false)
{
System.Console.WriteLine("Please enter a numeric argument.");
System.Console.WriteLine("Usage: Factorial <num>");
return 1;
}
long result = Functions.Factorial(num);
if(result == -1)
System.Console.WriteLine("Input must be > 0 and < 256.");
else
System.Console.WriteLine("The Factorial of {0} is {1}.", num, result);
return 0;
}
}