Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to get command line arguments for windows application in c#

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 08-07-2009
Member
 
Join Date: Apr 2009
Posts: 45
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....!

Reply With Quote
  #2  
Old 08-07-2009
Member
 
Join Date: Apr 2008
Posts: 1,947
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.
Reply With Quote
  #3  
Old 08-07-2009
Member
 
Join Date: Apr 2008
Posts: 2,000
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;            
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to get command line arguments for windows application in c#"
Thread Thread Starter Forum Replies Last Post
How can i get the command line parameters for any application/software? benno777 Software Development 1 03-07-2012 11:48 AM
Windows Phone 7 Error: Connection failed because of invalid command-line arguments Saqlain Portable Devices 3 28-10-2010 12:16 PM
Command Line Arguments in java Vipul03 Software Development 2 22-02-2010 03:39 PM
What are Command-Line Arguments in Java? Beter 2 Burn Out Software Development 8 20-02-2010 02:37 PM
PHP command line arguments Sori Software Development 3 24-09-2009 12:45 PM


All times are GMT +5.5. The time now is 12:14 PM.