방법: 디렉터리 목록 만들기

다음 코드 예제에서는 I/O 클래스를 사용하여 디렉터리에서 ".exe" 확장명을 가진 모든 파일의 목록을 만드는 방법을 보여 줍니다.

예제

Imports System
Imports System.IO

Public Class DirectoryLister
    Public Shared Sub Main(args() As String)
        Dim path As String = Environment.CurrentDirectory
        If args.Length > 0 Then
            If Directory.Exists(args(0)) Then
                path = args(0)
            Else
                Console.WriteLine("{0} not found; using current directory:",
                    args(0))
            End If
        End If
        Dim dir As New DirectoryInfo(path)
        For Each f As FileInfo In dir.GetFiles("*.exe")
            Dim name As String = f.Name
            Dim size As Long = f.Length
            Dim creationTime As DateTime = f.CreationTime
            Console.WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name)
        Next f
    End Sub
End Class
using System;
using System.IO;

public class DirectoryLister
{
    public static void Main(String[] args)
    {
        string path = Environment.CurrentDirectory;
        if (args.Length > 0)
        {
            if (Directory.Exists(args[0]))
            {
                path = args[0];
            }
            else
            {
                Console.WriteLine("{0} not found; using current directory:",
                    args[0]);
            }
        }
        DirectoryInfo dir = new DirectoryInfo(path);
        foreach (FileInfo f in dir.GetFiles("*.exe"))
        {
            string name = f.Name;
            long size = f.Length;
            DateTime creationTime = f.CreationTime;
            Console.WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name);
        }
    }
}
using namespace System;
using namespace System::IO;

public ref class DirectoryLister
{
public:
    static void Main(array<String^>^ args)
    {
        String^ path = Environment::CurrentDirectory;
        if (args->Length > 0)
        {
            if (Directory::Exists(args[0]))
            {
                path = args[0];
            }
            else
            {
                Console::WriteLine("{0} not found; using current directory:",
                    args[0]);
            }
        }
        DirectoryInfo^ dir = gcnew DirectoryInfo(path);
        for each (FileInfo^ f in dir->GetFiles("*.exe"))
        {
            String^ name = f->Name;
            long size = f->Length;
            DateTime^ creationTime = f->CreationTime;
            Console::WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name);
        }
    }
};

int main()
{
    DirectoryLister::Main(Environment::GetCommandLineArgs());
}

강력한 프로그래밍

이 예제에서 DirectoryInfo는 (".") 기호로 나타내는 현재 디렉터리이고 코드는 현재 디렉터리에서 확장명이 .exe인 모든 파일을 파일 크기, 만든 시간, 이름과 함께 나열합니다. C:\MyDir의 \Bin 하위 디렉터리에 .exe 파일이 있다고 가정하면 이 코드는 다음과 같이 출력됩니다.

953          7/20/2000 10:42 AM   C:\MyDir\Bin\paramatt.exe
664          7/27/2000 3:11 PM    C:\MyDir\Bin\tst.exe
403          8/8/2000 10:25 AM    C:\MyDir\Bin\dirlist.exe

C:\ 루트 디렉터리와 같은 다른 디렉터리에 파일 목록을 넣으려는 경우 이 코드(예: "testApplication.exe C:\")를 컴파일하여 생성된 실행 파일에 인수 "C:\"를 전달합니다.

참고참고

Visual Basic 사용자는 파일 I/O에 대해 FileSystem 클래스에서 제공하는 메서드와 속성을 사용하도록 선택할 수 있습니다.

참고 항목

작업

방법: 새로 만든 데이터 파일 읽기 및 쓰기

방법: 로그 파일 열기 및 추가

방법: 파일의 텍스트 읽기

방법: 파일에 텍스트 쓰기

방법: 문자열에서 문자 읽기

방법: 문자열에 문자 쓰기

참조

DirectoryInfo

CreationTime

FullName

FileInfo.Length

DirectoryInfo.GetFiles

개념

기본 파일 I/O