Directory::Exists Method (String^)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Determines whether the given path refers to an existing directory on disk.

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

static bool Exists(
	String^ path


Type: System::String^

The path to test.

Return Value

Type: System::Boolean

true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.

Trailing spaces are removed from the end of the path parameter before checking whether the directory exists.

The path parameter is not case-sensitive.

If you do not have at a minimum read-only permission to the directory, the Exists method will return false.

The Exists method 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 takes an array of file or directory names on the command line, determines what kind of name it is, and processes it appropriately.

// 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 );
      if ( Directory::Exists( path ) )

         // This path is a directory
         ProcessDirectory( path );
         Console::WriteLine( "{0} is not a valid file or directory.", path );



for reading files. Associated enumeration: FileIOPermissionAccess::Read

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