다음을 통해 공유


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

업데이트: 2007년 11월

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

예제

Option Explicit On 
Option Strict On
Imports System
Imports System.IO
Class DirectoryLister
    Public Shared Sub Main(ByVal args As String())
        Dim path As String = Environment.CurrentDirectory
        If (args.Length > 0) Then
            If File.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)
        Dim f As FileInfo
        For Each f 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;

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);
        }
    }

강력한 프로그래밍

이 예제에서 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에 대해 My.Computer.FileSystem 개체에서 제공하는 메서드와 속성을 사용하도록 선택할 수 있습니다. 자세한 내용은 My.Computer.FileSystem 개체를 참조하십시오.

참고 항목

작업

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

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

방법: 파일의 텍스트 읽기

방법: 파일에 텍스트 쓰기

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

방법: 문자열에 문자 쓰기

개념

기본 파일 I/O

참조

DirectoryInfo

CreationTime

FullName

FileInfo.Length

DirectoryInfo.GetFiles