Condividi tramite


Command-Line Arguments (C# Programming Guide)

You can send arguments to the Main method by defining the method in one of the following ways:

static int Main(string[] args)
static void Main(string[] args)

Note

To enable command-line arguments in the Main method in a Windows Forms application, you must manually modify the signature of Main in program.cs. The code generated by the Windows Forms designer creates a Main without an input parameter. You can also use Environment.CommandLine or Environment.GetCommandLineArgs to access the command-line arguments from any point in a console or Windows application.

The parameter of the Main method is a String array that represents the command-line arguments. Usually you determine whether arguments exist by testing the Length property, for example:

if (args.Length == 0)
{
    System.Console.WriteLine("Please enter a numeric argument.");
    return 1;
}

You can also convert the string arguments to numeric types by using the Convert class or the Parse method. For example, the following statement converts the string to a long number by using the Parse method:

long num = Int64.Parse(args[0]);

It is also possible to use the C# type long, which aliases Int64:

long num = long.Parse(args[0]);

You can also use the Convert class method ToInt64 to do the same thing:

long num = Convert.ToInt64(s);

For more information, see Parse and Convert.

Example

The following example shows how to use command-line arguments in a console application. The application 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 application issues a message that explains the correct usage of the program.

To compile and run the application from a command prompt, follow these steps:

  1. Paste the following code into any text editor, and then save the file as a text file with the name Factorial.cs.

    //Add a using directive for System if the directive isn't already present. 
    
    public class Functions
    {
        public static long Factorial(int n)
        {
            // Test for invalid input 
            if ((n < 0) || (n > 20))
            {
                return -1;
            }
    
            // Calculate the factorial iteratively rather than recursively: 
            long tempResult = 1;
            for (int i = 1; i <= n; i++)
            {
                tempResult *= i;
            }
            return tempResult;
        }
    }
    
    class MainClass
    {
        static int Main(string[] args)
        {
            // Test if input arguments were supplied: 
            if (args.Length == 0)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }
    
            // Try to convert the input arguments to numbers. This will throw 
            // an exception if the argument is not a number. 
            // num = int.Parse(args[0]); 
            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;
            }
    
            // Calculate factorial. 
            long result = Functions.Factorial(num);
    
            // Print result. 
            if (result == -1)
                System.Console.WriteLine("Input must be >= 0 and <= 20.");
            else
                System.Console.WriteLine("The Factorial of {0} is {1}.", num, result);
    
            return 0;
        }
    }
    // If 3 is entered on command line, the 
    // output reads: The factorial of 3 is 6.
    
  2. From the Start screen or Start menu, open a Visual Studio Developer Command Prompt window, and then navigate to the folder that contains the file that you just created.

  3. Enter the following command to compile the application.

    csc Factorial.cs

    If your application has no compilation errors, an executable file that's named Factorial.exe is created.

  4. Enter the following command to calculate the factorial of 3:

    Factorial 3

  5. The command produces this output: The factorial of 3 is 6.

Note

When running an application in Visual Studio, you can specify command-line arguments in the Debug Page, Project Designer.

For more examples about how to use command-line arguments, see How to: Create and Use Assemblies Using the Command Line (C# and Visual Basic).

See Also

Tasks

How to: Display Command Line Arguments (C# Programming Guide)

How to: Access Command-Line Arguments Using foreach (C# Programming Guide)

Reference

Main() Return Values (C# Programming Guide)

Classes (C# Programming Guide)

System.Environment

Concepts

C# Programming Guide

Main() and Command-Line Arguments (C# Programming Guide)