Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 */
public boolean get_Exists ()

public override function get Exists () : boolean

Not applicable.

Property Value

true if the directory exists; otherwise, false.

For a list of common I/O tasks, see Common I/O Tasks.

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

package DirectoryInfoJSL2; 

import System.*;
import System.IO.*;
   
class Class1
{
    // Copy a source directory to a target directory.
    public static void CopyDirectory(String SourceDirectory, 
        String TargetDirectory)
    {
        DirectoryInfo source = new DirectoryInfo(SourceDirectory);
        DirectoryInfo target = new DirectoryInfo(TargetDirectory);

        //Determine whether the source directory exists.
        if (!(source.get_Exists())) {
            return;
        }

        if (!(target.get_Exists())) {
            target.Create();
        }

        //Copy files.
        FileInfo sourceFiles[] = source.GetFiles();

        for (int i = 0; i < sourceFiles.length; i++) {
            File.Copy(sourceFiles[i].get_FullName(),
                target.get_FullName() + "\\" 
                + sourceFiles[i].get_Name(), true);
        }

        //Copy directories.
        DirectoryInfo sourceDirectories[] = source.GetDirectories();

        for (int j = 0; j < sourceDirectories.length; j++) {
            CopyDirectory(sourceDirectories[j].get_FullName(),
                target.get_FullName() + "\\" 
                + sourceDirectories[j].get_Name());
        }
    } //CopyDirectory

    public static void main(String[] args)
    {
        CopyDirectory("D:\\Tools", "D:\\NewTools");
    } //main
} //Class1

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();

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft