Skip to main content
.NET Framework Class Library
DirectoryExists Method

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

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Shared Function Exists ( _
	path As [%$TOPIC/c2756s2b_en-us_VS_110_1_0_0_0_0%] _
) As [%$TOPIC/c2756s2b_en-us_VS_110_1_0_0_0_1%]
public static [%$TOPIC/c2756s2b_en-us_VS_110_1_0_1_0_0%] Exists(
	[%$TOPIC/c2756s2b_en-us_VS_110_1_0_1_0_1%] path
)
public:
static [%$TOPIC/c2756s2b_en-us_VS_110_1_0_2_0_0%] Exists(
	[%$TOPIC/c2756s2b_en-us_VS_110_1_0_2_0_1%]^ path
)
static member Exists : 
        path:[%$TOPIC/c2756s2b_en-us_VS_110_1_0_3_0_0%] -> [%$TOPIC/c2756s2b_en-us_VS_110_1_0_3_0_1%]

Parameters

path
Type: SystemString

The path to test.

Return Value

Type: SystemBoolean
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 file exists.
Remarks

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.

Examples

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  

Imports System
Imports System.IO
Imports System.Collections

Public Class RecursiveFileProcessor

    Public Overloads Shared Sub Main(ByVal args() As String)
        Dim path As String 
        For Each path In args
            If File.Exists(path) Then 
                ' This path is a file.
                ProcessFile(path)
            Else 
                If Directory.Exists(path) Then 
                    ' This path is a directory.
                    ProcessDirectory(path)
                Else
                    Console.WriteLine("{0} is not a valid file or directory.", path)
                End If 
            End If 
        Next path
    End Sub 'Main


    ' Process all files in the directory passed in, recurse on any directories  
    ' that are found, and process the files they contain. 
    Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory. 
        Dim fileName As String 
        For Each fileName In fileEntries
            ProcessFile(fileName)

        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory. 
        Dim subdirectory As String 
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        Next subdirectory

    End Sub 'ProcessDirectory

    ' Insert logic for processing found files here. 
    Public Shared Sub ProcessFile(ByVal path As String)
        Console.WriteLine("Processed file '{0}'.", path)
    End Sub 'ProcessFile
End Class 'RecursiveFileProcessor
// For File.Exists, Directory.Exists 
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, recurse on any directories  
    // that are found, and process the files they contain. 
    public static void ProcessDirectory(string targetDirectory) 
    {
        // Process the list of files found in the directory. 
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory. 
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }

    // Insert logic for processing found files here. 
    public static void ProcessFile(string path) 
    {
        Console.WriteLine("Processed file '{0}'.", path);	    
    }
}
// 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 );
      }
      else 
      if ( Directory::Exists( path ) )
      {

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

   }
}
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
.NET Framework Security
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.