DirectoryInfo::Exists Property
.NET Framework (current version)
Gets a value indicating whether the directory exists.
Assembly: mscorlib (in mscorlib.dll)
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 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" ); }
Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Show: