본문 바로가기
컴퓨터/C#

[C#] Main문 매개변수 args 사용해보기, ERRORLEVEL 환경변수로 return값 확인

by 아기상어  2020. 5. 31.

 

 

cmd창 커맨드로 args에 값을 적어 보낼수 있다.

 

Console.WriteLine(args.Length);

 

쳐서 Program 1 2 3 4 5 를 입력하면 5가 출력된다.

 

이걸 이용해서 몇개의 문자열을 쳤는지와  각각의 문자열을 출력하게 할수도 있다.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace _20200529_004
{
    class Program
    { 
        static void Main(string[] args)
        {
            for (int iNum = 0; iNum < args.Length; iNum++)
            {
                Console.WriteLine("[{0}] : {1}", iNum, args[iNum]);
            }
        }
    }
}

 

 

 

 

 

int형 Main문 끝에 return 변수값; 을 쓰면 echo %ERRORLEVEL% 환경변수를 이용해 무슨값이 리턴되는지 알 수 있다.