Freigeben über


Directory.Exists-Methode

Bestimmt, ob der angegebene Pfad auf ein vorhandenes Verzeichnis auf einem Datenträger verweist.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Exists ( _
    path As String _
) As Boolean
'Usage
Dim path As String
Dim returnValue As Boolean

returnValue = Directory.Exists(path)
public static bool Exists (
    string path
)
public:
static bool Exists (
    String^ path
)
public static boolean Exists (
    String path
)
public static function Exists (
    path : String
) : boolean

Parameter

  • path
    Der zu testende Pfad.

Rückgabewert

true, wenn path auf ein vorhandenes Verzeichnis verweist, andernfalls false.

Hinweise

Mit dem path-Parameter dürfen relative oder absolute Pfadinformationen angegeben werden. Relative Pfadinformationen werden relativ zum aktuellen Arbeitsverzeichnis interpretiert.

Nachgestellte Leerzeichen am Ende des path-Parameters werden entfernt, bevor überprüft wird, ob das Verzeichnis vorhanden ist.

Beim path-Parameter wird nicht zwischen Groß- und Kleinschreibung unterschieden.

Die Exists-Methode führt keine Netzwerkauthentifizierung aus. Wenn Sie eine vorhandene Netzwerkfreigabe ohne vorherige Authentifizierung abfragen, gibt die Exists-Methode false zurück.

In der folgenden Tabelle sind Beispiele für andere typische oder verwandte E/A-Aufgaben aufgeführt.

Aufgabe

Beispiel in diesem Thema

Erstellen einer Textdatei.

Gewusst wie: Schreiben von Text in eine Datei

Schreiben in eine Textdatei.

Gewusst wie: Schreiben von Text in eine Datei

Lesen aus einer Textdatei.

Gewusst wie: Lesen aus einer Textdatei

Umbenennen oder Verschieben eines Verzeichnisses.

Directory.Move

DirectoryInfo.MoveTo

Löschen eines Verzeichnisses.

Directory.Delete

DirectoryInfo.Delete

Erstellen eines Verzeichnisses.

CreateDirectory

Directory

Erstellen eines Unterverzeichnisses.

CreateSubdirectory

Anzeigen der Dateien in einem Verzeichnis.

Name

Anzeigen der Unterverzeichnisse in einem Verzeichnis.

GetDirectories

GetDirectories

Anzeigen aller Dateien in allen Unterverzeichnissen eines Verzeichnisses.

GetFileSystemInfos

Ermitteln der Größe eines Verzeichnisses.

Directory

Bestimmen, ob eine Datei vorhanden ist.

Exists

Sortieren von Dateien in einem Verzeichnis nach Größe.

GetFileSystemInfos

Beispiel

Im folgenden Codebeispiel wird bei einem Array von Datei- oder Verzeichnisnamen in der Befehlszeile die Art des Namens bestimmt, und das Array wird entsprechend verarbeitet.

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

   }
}
// For File.Exists, Directory.Exists
import System.*;
import System.IO.*;
import System.Collections.*;

public class RecursiveFileProcessor
{
    public static void main(String[] args)
    {
        for (int iCtr = 0; iCtr < args.get_Length(); iCtr++) {
            String path = args[iCtr];
            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);
                }
            }
        }
    } //main

    // 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);
        for (int iCtr1 = 0; iCtr1 < fileEntries.get_Length(); iCtr1++) {
            String fileName = fileEntries[iCtr1];
            ProcessFile(fileName);
        }
        // Recurse into subdirectories of this directory.
        String subDirectoryEntries[] = 
            Directory.GetDirectories(targetDirectory);
        for (int iCtr2 = 0; iCtr2 < subDirectoryEntries.get_Length(); 
            iCtr2++) {
            String subDirectory = subDirectoryEntries[iCtr2];
            ProcessDirectory(subDirectory);
        }
    } //ProcessDirectory

    // Insert logic for processing found files here.
    public static void ProcessFile(String path)
    {
        Console.WriteLine("Processed file '{0}'.", path);
    } //ProcessFile
} //RecursiveFileProcessor
//For Directory.GetFiles and Directory.GetDirectories
import System;
import System.IO;
import System.Collections;

// For File.Exists, Directory.Exists 
// Takes an array of file names or directory names on the command line.  
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
    public static function Main(args : String[]) : void  {
        for(var i : int in args) {
            var path : String = args[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);
            }        
        }        
    }


    // Process all files in the directory passed in, and recurse on any directories 
    // that are found to process the files they contain
    public static function ProcessDirectory(targetDirectory : String) : void  {
        // Process the list of files found in the directory
        var fileEntries : String [] = Directory.GetFiles(targetDirectory);
        for (var i : int in fileEntries)
            ProcessFile(fileEntries[i]);

        // Recurse into subdirectories of this directory
        var subdirectoryEntries : String[] = Directory.GetDirectories(targetDirectory);
        for (i in subdirectoryEntries)
            ProcessDirectory(subdirectoryEntries[i]);
    }
        
    // Real logic for processing found files would go here.
    public static function ProcessFile(path : String) : void  {
        Console.WriteLine("Processed file '{0}'.", path);       
    }
}

// For JScript there is no 'Main' routine defined and hence the command line arguments
// have to be obtained with a call to System.Environment.GetCommandLineArgs
RecursiveFileProcessor.Main(System.Environment.GetCommandLineArgs());

.NET Framework-Sicherheit

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Directory-Klasse
Directory-Member
System.IO-Namespace
DirectoryInfo

Weitere Ressourcen

Datei- und Stream-E/A
Gewusst wie: Lesen aus einer Textdatei
Gewusst wie: Schreiben von Text in eine Datei