다음을 통해 공유


DirectoryInfo.Exists 속성

정의

디렉터리가 있는지를 나타내는 값을 가져옵니다.

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

속성 값

디렉터리가 있으면 true이고, 그렇지 않으면 false입니다.

예제

다음 예에서는 원본 디렉터리를 대상 디렉터리에 복사하는 컨텍스트에서 속성을 사용하는 Exists 방법을 보여 줍니다.

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

   if (  !target->Exists )
      target->Create();

   
   //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.
            if(!source.Exists)
                return;
            if(!target.Exists)
                target.Create();
            
            //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)
        {
            CopyDirectory("D:\\Tools","D:\\NewTools");
        }
    }
}
open System.IO

// Copy a source directory to a target directory.
let rec copyDirectory sourceDirectory targetDirectory =
    let source = DirectoryInfo sourceDirectory
    let target = DirectoryInfo targetDirectory
    
    //Determine whether the source directory exists.
    if source.Exists then
        if target.Exists then
            target.Create()
        
        //Copy files.
        let sourceFiles = source.GetFiles()
        for file in sourceFiles do
            File.Copy(file.FullName, target.FullName + "\\" + file.Name,true)
        
        //Copy directories.
        let sourceDirectories = source.GetDirectories()
        for dir in sourceDirectories do
            copyDirectory dir.FullName (target.FullName + "\\" + dir.Name)

copyDirectory "D:\\Tools" "D:\\NewTools"
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
            Return
        End If
        If (target.Exists = False) Then
            target.Create()
        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 됩니다. 잘못된 문자 또는 너무 많은 문자, 실패 또는 누락된 디스크를 사용하여 파일 이름을 전달하거나 호출자에게 파일을 읽을 수 있는 권한이 없는 경우와 같은 예외가 발생할 수 있습니다.

적용 대상

추가 정보