프로그래밍 노트

C#| 지정 디렉토리 밑의 파일명 가져오는 소스 예제 본문

C#/기초

C#| 지정 디렉토리 밑의 파일명 가져오는 소스 예제

떡잎 2012. 12. 13. 23:13


using System;
using System.IO;

namespace DirectoryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("디렉토리명 입력 안함");
                return;
            }

            // 지정 디렉토리의 유무 확인
            if (!Directory.Exists(args[0]))
            {
                Console.WriteLine("찾는 디렉토리 없음");
                return;
            }

            DirectoryInfo dir = new DirectoryInfo(args[0]);
            FileInfo[] files = dir.GetFiles();

            // foreach 배열의 갯수를 모를 때, 모든 배열만큼 돈다. 
            foreach (FileInfo file in files)
            {
                // {0,-32} 자리 32칸 확보
                // {1,16:N0} 16칸을 확보하여 소숫점 밑은 제거(뒷자리로 정렬)
                Console.WriteLine("{0,-32} {1,16:N0} {2}", 
                    file.CreationTime, file.Length, file.Name);

            }
            Console.ReadLine();
        }
    }
}



Comments