명령줄 인수(C# 프로그래밍 가이드)

업데이트: 2007년 11월

Main 메서드에는 인수를 사용할 수 있으며 다음 형식을 사용합니다.

static int Main(string[] args)
static void Main(string[] args)
참고:

Windows Forms 응용 프로그램의 Main 메서드에서 명령줄 인수를 사용하려면 program.cs에서 Main 시그니처를 직접 수정해야 합니다. Windows Forms 디자이너에서 생성된 코드에서는 입력 매개 변수가 없는 상태로 Main을 만듭니다. 또한 Environment.CommandLine 또는 Environment.GetCommandLineArgs를 사용하여 콘솔 또는 Windows 응용 프로그램에서 언제든지 명령줄 인수에 액세스할 수 있습니다.

Main 메서드의 매개 변수는 명령줄 인수를 나타내는 String 배열입니다. 일반적으로 다음 예제와 같이 Length 속성을 테스트하여 인수가 있는지 확인합니다.

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

또한 Convert 클래스나 Parse 메서드를 사용하여 문자열 인수를 숫자 형식으로 변환할 수 있습니다. 예를 들어 다음 문에서는 Parse 메서드를 사용하여 string을 long 숫자로 변환합니다.

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

또한 Int64의 별칭인 C#의 long 형식을 사용할 수도 있습니다.

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

또한 Convert 클래스 메서드 ToInt64를 사용할 수도 있습니다.

long num = Convert.ToInt64(s);

자세한 내용은 ParseConvert를 참조하십시오.

예제

다음 예제에서는 콘솔 응용 프로그램에서 명령줄 인수를 사용하는 방법을 보여 줍니다. 이 프로그램에서는 런타임에 하나의 인수를 받아들여 해당 인수를 정수로 변환하며 숫자의 계승을 계산합니다. 인수를 제공하지 않으면 프로그램에서 올바른 사용법을 설명하는 메시지를 표시합니다.

참고:

Visual Studio에서 응용 프로그램을 실행할 때 프로젝트 디자이너, 디버그 페이지에서 명령줄 인수를 지정할 수 있습니다.

명령줄 인수 사용 방법에 대한 예제를 좀 더 보려면 방법: C# DLL 만들기 및 사용(C# 프로그래밍 가이드)을 참조하십시오.

public class Functions
{
    public static long Factorial(int n)
    {
        // Test for invalid input
        if ((n <= 0) || (n > 256))
        { 
            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 < 256.");
        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.

참고 항목

작업

방법: 명령줄 인수 표시(C# 프로그래밍 가이드)

방법: foreach를 사용하여 명령줄 인수 액세스(C# 프로그래밍 가이드)

개념

C# 프로그래밍 가이드

Main()과 명령줄 인수(C# 프로그래밍 가이드)

참조

Main() 반환 값(C# 프로그래밍 가이드)

클래스(C# 프로그래밍 가이드)

System.Environment