Exists Property
Collapse the table of content
Expand the table of content

DirectoryInfo.Exists Property


Gets a value indicating whether the directory exists.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public override bool Exists { get; }

Property Value

Type: System.Boolean

true if the directory exists; otherwise, 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.

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

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)

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft