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 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"); } } }
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: