다음을 통해 공유


Directory.Exists 메서드

지정된 경로가 디스크에 있는 기존 디렉터리를 참조하는지 여부를 확인합니다.

네임스페이스: System.IO
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Shared Function Exists ( _
    path As String _
) As Boolean
‘사용 방법
Dim path As String
Dim returnValue As Boolean

returnValue = Directory.Exists(path)
public static bool Exists (
    string path
)
public:
static bool Exists (
    String^ path
)
public static boolean Exists (
    String path
)
public static function Exists (
    path : String
) : boolean

매개 변수

  • path
    테스트할 경로입니다.

반환 값

path가 기존 디렉터리를 참조하면 true이고, 그렇지 않으면 false입니다.

설명

path 매개 변수에는 상대 경로나 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리에 상대적으로 해석됩니다.

디렉터리가 있는지 여부를 검사하기 전에 path 매개 변수 끝에 있는 후행 공백이 제거됩니다.

path 매개 변수는 대/소문자를 구분하지 않습니다.

Exists 메서드는 네트워크 인증을 수행하지 않습니다. 미리 인증받지 않고 네트워크 공유를 쿼리하면 Exists 메서드가 false를 반환합니다.

다음 표에서는 일반적인 예 또는 관련된 I/O 작업의 예를 보여 줍니다.

수행 작업

참조 항목

텍스트 파일을 만듭니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에 씁니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에서 읽습니다.

방법: 파일의 텍스트 읽기

디렉터리 이름을 바꾸거나 이동합니다.

Directory.Move

DirectoryInfo.MoveTo

디렉터리를 삭제합니다.

Directory.Delete

DirectoryInfo.Delete

디렉터리를 만듭니다.

CreateDirectory

Directory

하위 디렉터리를 만듭니다.

CreateSubdirectory

디렉터리의 파일을 참조하십시오.

Name

디렉터리의 하위 디렉터리를 참조하십시오.

GetDirectories

GetDirectories

디렉터리의 전체 하위 디렉터리에 있는 모든 파일을 참조하십시오.

GetFileSystemInfos

디렉터리의 크기를 찾습니다.

Directory

파일이 있는지 여부를 확인합니다.

Exists

디렉터리의 파일을 크기순으로 정렬합니다.

GetFileSystemInfos

예제

다음 코드 예제에서는 명령줄에서 파일 또는 디렉터리 이름의 배열을 사용하여 이름의 유형을 확인한 후 적절하게 처리합니다.

' For File.Exists, Directory.Exists 

Imports System
Imports System.IO
Imports System.Collections

Public Class RecursiveFileProcessor

    Public Overloads Shared Sub Main(ByVal args() As String)
        Dim path As String
        For Each path In args
            If File.Exists(path) Then
                ' This path is a file.
                ProcessFile(path)
            Else
                If Directory.Exists(path) Then
                    ' This path is a directory.
                    ProcessDirectory(path)
                Else
                    Console.WriteLine("{0} is not a valid file or directory.", path)
                End If
            End If
        Next path
    End Sub 'Main


    ' Process all files in the directory passed in, recurse on any directories 
    ' that are found, and process the files they contain.
    Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory.
        Dim fileName As String
        For Each fileName In fileEntries
            ProcessFile(fileName)

        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory.
        Dim subdirectory As String
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        Next subdirectory

    End Sub 'ProcessDirectory

    ' Insert logic for processing found files here.
    Public Shared Sub ProcessFile(ByVal path As String)
        Console.WriteLine("Processed file '{0}'.", path)
    End Sub 'ProcessFile
End Class 'RecursiveFileProcessor
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory) 
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }
        
    // Insert logic for processing found files here.
    public static void ProcessFile(string path) 
    {
        Console.WriteLine("Processed file '{0}'.", path);       
    }
}
// For File::Exists, Directory::Exists
using namespace System;
using namespace System::IO;
using namespace System::Collections;

// Insert logic for processing found files here.
void ProcessFile( String^ path )
{
   Console::WriteLine( "Processed file '{0}'.", path );
}


// Process all files in the directory passed in, recurse on any directories 
// that are found, and process the files they contain.
void ProcessDirectory( String^ targetDirectory )
{
   
   // Process the list of files found in the directory.
   array<String^>^fileEntries = Directory::GetFiles( targetDirectory );
   IEnumerator^ files = fileEntries->GetEnumerator();
   while ( files->MoveNext() )
   {
      String^ fileName = safe_cast<String^>(files->Current);
      ProcessFile( fileName );
   }

   
   // Recurse into subdirectories of this directory.
   array<String^>^subdirectoryEntries = Directory::GetDirectories( targetDirectory );
   IEnumerator^ dirs = subdirectoryEntries->GetEnumerator();
   while ( dirs->MoveNext() )
   {
      String^ subdirectory = safe_cast<String^>(dirs->Current);
      ProcessDirectory( subdirectory );
   }
}

int main( int argc, char *argv[] )
{
   for ( int i = 1; i < argc; i++ )
   {
      String^ path = gcnew String(argv[ i ]);
      if ( File::Exists( path ) )
      {
         
         // This path is a file
         ProcessFile( path );
      }
      else
      if ( Directory::Exists( path ) )
      {
         
         // This path is a directory
         ProcessDirectory( path );
      }
      else
      {
         Console::WriteLine( "{0} is not a valid file or directory.", path );
      }

   }
}
// For File.Exists, Directory.Exists
import System.*;
import System.IO.*;
import System.Collections.*;

public class RecursiveFileProcessor
{
    public static void main(String[] args)
    {
        for (int iCtr = 0; iCtr < args.get_Length(); iCtr++) {
            String path = args[iCtr];
            if (File.Exists(path)) {
                // This path is a file
                ProcessFile(path);
            }
            else {
                if (Directory.Exists(path)) {
                    // This path is a directory
                    ProcessDirectory(path);
                }
                else {
                    Console.WriteLine("{0} is not a valid file or directory.",
                        path);
                }
            }
        }
    } //main

    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public static void ProcessDirectory(String targetDirectory)
    {
        // Process the list of files found in the directory.
        String fileEntries[] = Directory.GetFiles(targetDirectory);
        for (int iCtr1 = 0; iCtr1 < fileEntries.get_Length(); iCtr1++) {
            String fileName = fileEntries[iCtr1];
            ProcessFile(fileName);
        }
        // Recurse into subdirectories of this directory.
        String subDirectoryEntries[] = 
            Directory.GetDirectories(targetDirectory);
        for (int iCtr2 = 0; iCtr2 < subDirectoryEntries.get_Length(); 
            iCtr2++) {
            String subDirectory = subDirectoryEntries[iCtr2];
            ProcessDirectory(subDirectory);
        }
    } //ProcessDirectory

    // Insert logic for processing found files here.
    public static void ProcessFile(String path)
    {
        Console.WriteLine("Processed file '{0}'.", path);
    } //ProcessFile
} //RecursiveFileProcessor
//For Directory.GetFiles and Directory.GetDirectories
import System;
import System.IO;
import System.Collections;

// For File.Exists, Directory.Exists 
// Takes an array of file names or directory names on the command line.  
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
    public static function Main(args : String[]) : void  {
        for(var i : int in args) {
            var path : String = args[i];
            if (File.Exists(path)) {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) {
                // This path is a directory
                ProcessDirectory(path);
            }
            else {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, and recurse on any directories 
    // that are found to process the files they contain
    public static function ProcessDirectory(targetDirectory : String) : void  {
        // Process the list of files found in the directory
        var fileEntries : String [] = Directory.GetFiles(targetDirectory);
        for (var i : int in fileEntries)
            ProcessFile(fileEntries[i]);

        // Recurse into subdirectories of this directory
        var subdirectoryEntries : String[] = Directory.GetDirectories(targetDirectory);
        for (i in subdirectoryEntries)
            ProcessDirectory(subdirectoryEntries[i]);
    }
        
    // Real logic for processing found files would go here.
    public static function ProcessFile(path : String) : void  {
        Console.WriteLine("Processed file '{0}'.", path);       
    }
}

// For JScript there is no 'Main' routine defined and hence the command line arguments
// have to be obtained with a call to System.Environment.GetCommandLineArgs
RecursiveFileProcessor.Main(System.Environment.GetCommandLineArgs());

.NET Framework 보안

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Directory 클래스
Directory 멤버
System.IO 네임스페이스
DirectoryInfo

기타 리소스

파일 및 스트림 I/O
방법: 파일의 텍스트 읽기
방법: 파일에 텍스트 쓰기