DirectoryInfo.Exists Property


디렉터리가 있는지를 나타내는 값을 가져옵니다.Gets a value indicating whether the directory exists.

 virtual property bool Exists { bool get(); };
public override bool Exists { get; }
member this.Exists : bool
Public Overrides ReadOnly Property Exists As Boolean

Property Value


디렉터리가 있으면 true이고, 그렇지 않으면 false입니다.true if the directory exists; otherwise, false.


다음 예제에서는 원본 디렉터리를 대상 디렉터리에 복사 하는 컨텍스트에서 Exists 속성을 사용 하는 방법을 보여 줍니다.The following example demonstrates a use of the Exists property in the context of copying a source directory to a target directory.

using namespace System;
using namespace System::IO;

// Copy a source directory to a target directory.
void CopyDirectory( String^ SourceDirectory, String^ TargetDirectory )
   DirectoryInfo^ source = gcnew DirectoryInfo( SourceDirectory );
   DirectoryInfo^ target = gcnew DirectoryInfo( TargetDirectory );
   //Determine whether the source directory exists.
   if (  !source->Exists )

   if (  !target->Exists )

   //Copy files.
   array<FileInfo^>^sourceFiles = source->GetFiles();
   for ( int i = 0; i < sourceFiles->Length; ++i )
      File::Copy( sourceFiles[ i ]->FullName, String::Concat( target->FullName, "\\", sourceFiles[ i ]->Name ), true );
   //Copy directories.
   array<DirectoryInfo^>^sourceDirectories = source->GetDirectories();
   for ( int j = 0; j < sourceDirectories->Length; ++j )
      CopyDirectory( sourceDirectories[ j ]->FullName, String::Concat( target->FullName, "\\", sourceDirectories[ j ]->Name ) );

int main()
   CopyDirectory( "D:\\Tools", "D:\\NewTools" );

using System;
using System.IO;

namespace DirectoryInfoCS2
    class Class1
        // Copy a source directory to a target directory.
        static public void CopyDirectory(string SourceDirectory, string TargetDirectory)
            DirectoryInfo	source = new DirectoryInfo(SourceDirectory);
            DirectoryInfo	target = new DirectoryInfo(TargetDirectory);
            //Determine whether the source directory exists.
            //Copy files.
            FileInfo[] sourceFiles = source.GetFiles();	
            for(int i = 0; i < sourceFiles.Length; ++i)
                File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name,true);
            //Copy directories.
            DirectoryInfo[] sourceDirectories = source.GetDirectories();	
            for(int j = 0; j < sourceDirectories.Length; ++j)
                CopyDirectory(sourceDirectories[j].FullName,target.FullName +"\\" + sourceDirectories[j].Name);

        static void Main(string[] args)
Imports System.IO

Module Module1
    Public Sub CopyDirectory(ByVal SourceDirectory As String, ByVal TargetDirectory As String)
        Dim source As DirectoryInfo = New DirectoryInfo(SourceDirectory)
        Dim target As DirectoryInfo = New DirectoryInfo(TargetDirectory)

        'Determine whether the source directory exists.
        If (source.Exists = False) Then
        End If
        If (target.Exists = False) Then
        End If

        'Copy files.
        Dim sourceFiles As FileInfo() = source.GetFiles()
        Dim i, j As Integer
        For i = 0 To sourceFiles.Length - 1
            File.Copy(sourceFiles(i).FullName, target.FullName + "\\" + sourceFiles(i).Name, True)
        Next i

        'Copy directories.
        Dim sourceDirectories As DirectoryInfo() = source.GetDirectories()
        For j = 0 To sourceDirectories.Length - 1
            CopyDirectory(sourceDirectories(j).FullName, target.FullName + "\\" + sourceDirectories(j).Name)
        Next j
        source = Nothing
        target = Nothing
    End Sub
    Sub Main()
        CopyDirectory("D:\\Tools", "D:\\NewTools")
    End Sub
End Module


Exists 속성은 지정 된 파일이 있는지 확인 하는 동안 오류가 발생 하는 경우 false을 반환 합니다.The Exists property returns false if any error occurs while trying to determine if the specified file exists. 잘못 된 문자 또는 실패 했거나 누락 된 디스크에 너무 많은 문자를 사용 하 여 파일 이름을 전달할 때 예외를 발생 시키는 상황에서 발생할 수 있습니다 또는 호출자에 게 파일을 읽을 수 있는 사용 권한이 없습니다.This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

Applies to

See also