This documentation is archived and is not being maintained.

DirectoryInfo.Exists Property

Gets a value indicating whether the directory exists.

[Visual Basic]
Overrides Public ReadOnly Property Exists As Boolean
[C#]
public override bool Exists {get;}
[C++]
public: __property bool get_Exists();
[JScript]
public override function get Exists() : Boolean;

Property Value

true if the directory exists; otherwise, false.

Remarks

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Copy a directory. Directory
Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Delete a directory. Directory.Delete

DirectoryInfo.Delete

Create a directory. CreateDirectory

Directory

Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
See the subdirectories of a directory. GetDirectories

GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos
Find the size of a directory. Directory
Determine if a file exists. Exists
Sort files in a directory by size. GetFileSystemInfos

Example

The following example demonstrates a use of the Exists property in the context of copying a source directory to a target directory.

[Visual Basic] 
Imports System
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

[C#] 
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");
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

// Copy a source directory to a target directory.

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, String::Concat( target->FullName, S"\\", sourceFiles[i]->Name ), true);

    //Copy directories.
    DirectoryInfo* sourceDirectories[] = source->GetDirectories();    
    for(int j = 0; j < sourceDirectories->Length; ++j)
        CopyDirectory(sourceDirectories[j]->FullName, String::Concat( target->FullName, S"\\", sourceDirectories[j]->Name ));
}

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

[JScript] 
import System;
import System.IO;

package DirectoryInfoCS2
{
    class Class1
    {
        static public function CopyDirectory(SourceDirectory : String, TargetDirectory : String )
        {
            var source : DirectoryInfo = new DirectoryInfo(SourceDirectory);
            var target : DirectoryInfo = new DirectoryInfo(TargetDirectory);
            
            //Determine whether the source file exists.
            if(!source.Exists)
                return;
            if(!target.Exists)
                target.Create();
            
            //Copy files.
            var sourceFiles : FileInfo[] = source.GetFiles();    
            for(var i : int = 0; i < sourceFiles.Length; ++i)
                File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name,true);
            
            //Copy directories.
            var sourceDirectories : DirectoryInfo[] = source.GetDirectories();    
            for(var j : int = 0; j < sourceDirectories.Length; ++j)
                CopyDirectory(sourceDirectories[j].FullName,target.FullName +"\\" + sourceDirectories[j].Name);
        }
        
        static function Main(args : String[])
        {
            CopyDirectory("D:\\Tools","D:\\NewTools");
        }
    }
}

DirectoryInfoCS2.Class1.Main();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show: