Freigeben über


FileInfo.Directory-Eigenschaft

Ruft eine Instanz des übergeordneten Verzeichnisses ab.

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

Syntax

'Declaration
Public ReadOnly Property Directory As DirectoryInfo
'Usage
Dim instance As FileInfo
Dim value As DirectoryInfo

value = instance.Directory
public DirectoryInfo Directory { get; }
public:
property DirectoryInfo^ Directory {
    DirectoryInfo^ get ();
}
/** @property */
public DirectoryInfo get_Directory ()
public function get Directory () : DirectoryInfo

Eigenschaftenwert

Ein DirectoryInfo-Objekt, das das übergeordnete Verzeichnis dieser Datei darstellt.

Ausnahmen

Ausnahmetyp Bedingung

DirectoryNotFoundException

Der angegebene Pfad ist ungültig. Dies ist z. B. der Fall, wenn das Laufwerk des Pfads nicht zugeordnet ist.

SecurityException

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

Hinweise

Zum Abrufen des übergeordneten Verzeichnisses als Zeichenfolge verwenden Sie die DirectoryName-Eigenschaft.

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

Aufgabe

Beispiel in diesem Thema

Kopieren eines Verzeichnisses.

Directory

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

Die Unterverzeichnisse eines Verzeichnisses anzeigen.

GetDirectories

GetDirectories

Alle Dateien in allen Unterverzeichnissen eines Verzeichnisses anzeigen.

GetFileSystemInfos

Die Größe eines Verzeichnisses ermitteln.

Directory

Bestimmen, ob eine Datei vorhanden ist.

Exists

Bestimmen, ob ein Verzeichnis vorhanden ist.

Exists

Beispiel

Im folgenden Beispiel wird eine Datei geöffnet bzw. erstellt, ihr vollständiger Pfad wird bestimmt, und der vollständige Inhalt des Verzeichnisses wird ermittelt und angezeigt.

Imports System
Imports System.IO

Public Class DirectoryTest

    Public Shared Sub Main()
        ' Open an existing file, or create a new one.
        Dim fi As New FileInfo("temp.txt")

        ' Determine the full path of the file just created.
        Dim di As DirectoryInfo = fi.Directory

        ' Figure out what other entries are in that directory.
        Dim fsi As FileSystemInfo() = di.GetFileSystemInfos()

        ' Print the names of all the files and subdirectories of that directory.
        Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName)
        Dim info As FileSystemInfo
        For Each info In fsi
            Console.WriteLine(info.Name)
        Next info
    End Sub 'Main
End Class 'DirectoryTest
using System;
using System.IO;

public class DirectoryTest 
{
    public static void Main() 
    {

        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Determine the full path of the file just created.
        DirectoryInfo di = fi.Directory;

        // Figure out what other entries are in that directory.
        FileSystemInfo[] fsi = di.GetFileSystemInfos();

        Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);

        // Print the names of all the files and subdirectories of that directory.
        foreach (FileSystemInfo info in fsi)
            Console.WriteLine(info.Name);
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   
   // Open an existing file, or create a new one.
   FileInfo^ fi = gcnew FileInfo( "temp.txt" );
   
   // Determine the full path of the file just created.
   DirectoryInfo^ di = fi->Directory;
   
   // Figure out what other entries are in that directory.
   array<FileSystemInfo^>^fsi = di->GetFileSystemInfos();
   Console::WriteLine( "The directory '{0}' contains the following files and directories:", di->FullName );
   
   // Print the names of all the files and subdirectories of that directory.
   Collections::IEnumerator^ myEnum = fsi->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      FileSystemInfo^ info = safe_cast<FileSystemInfo^>(myEnum->Current);
      Console::WriteLine( info->Name );
   }
}
import System.*;
import System.IO.*;

public class DirectoryTest
{
    public static void main(String[] args)
    {
        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Determine the full path of the file just created.
        DirectoryInfo di = fi.get_Directory();

        // Figure out what other entries are in that directory.
        FileSystemInfo fsi[] = di.GetFileSystemInfos();
        Console.WriteLine("The directory '{0}' contains the following files " 
            + " and directories:", di.get_FullName());

        // Print the names of all the files and subdirectories of
        // that directory.
        for (int iCtr = 0; iCtr < fsi.length; iCtr++) {
            FileSystemInfo info = (FileSystemInfo)fsi.get_Item(iCtr);
            Console.WriteLine(info.get_Name());
        }
    } //main
} //DirectoryTest
import System;
import System.IO;

public class DirectoryTest {
    public static function Main() : void {

        // Open an existing file, or create a new one.
        var fi : FileInfo = new FileInfo("temp.txt");

        // Determine the full path of the file just created.
        var di : DirectoryInfo = fi.Directory;

        // Figure out what other entries are in that directory.
        var fsi : FileSystemInfo[] = di.GetFileSystemInfos();

        Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);

        // Print the names of all the files and subdirectories of that directory.
        for (var i : int in fsi)
            Console.WriteLine(fsi[i].Name);
    }
}
DirectoryTest.Main();

.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

FileInfo-Klasse
FileInfo-Member
System.IO-Namespace

Weitere Ressourcen

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