Results 1 to 3 of 3

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

  1. #1
    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....!

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    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.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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;            
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 03-07-2012, 11:48 AM
  2. Replies: 3
    Last Post: 28-10-2010, 12:16 PM
  3. Command Line Arguments in java
    By Vipul03 in forum Software Development
    Replies: 2
    Last Post: 22-02-2010, 04:39 PM
  4. What are Command-Line Arguments in Java?
    By Beter 2 Burn Out in forum Software Development
    Replies: 8
    Last Post: 20-02-2010, 03:37 PM
  5. PHP command line arguments
    By Sori in forum Software Development
    Replies: 3
    Last Post: 24-09-2009, 12:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,059,638.83381 seconds with 17 queries