Windows apps
Collapse the table of content
Expand the table of content
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

Property Value

true if the directory exists; otherwise, false.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Rename or move a directory.

Directory.Move

DirectoryInfo.MoveTo

Delete a directory.

Directory.Delete

DirectoryInfo.Delete

Create a directory.

CreateDirectory

Directory

Create a subdirectory.

CreateSubdirectory

See the files in a directory.

Name

See the subdirectories of a directory.

GetDirectories

GetDirectories

See all the files and all the subdirectories in a directory.

GetFileSystemInfos

Find the size of a directory.

Directory

Determine if a file exists.

Exists

Sort files in a directory by size.

GetFileSystemInfos

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2016 Microsoft