Directory.GetParent(String) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ruft das übergeordnete Verzeichnis des angegebenen Pfads ab, sowohl für absolute als auch für relative Pfade.
public:
static System::IO::DirectoryInfo ^ GetParent(System::String ^ path);
public static System.IO.DirectoryInfo GetParent (string path);
public static System.IO.DirectoryInfo? GetParent (string path);
static member GetParent : string -> System.IO.DirectoryInfo
Public Shared Function GetParent (path As String) As DirectoryInfo
Parameter
- path
- String
Der Pfad, dessen übergeordnetes Verzeichnis abgerufen werden soll.
Gibt zurück
Das übergeordnete Verzeichnis oder null
, wenn path
das Stammverzeichnis ist, einschließlich des Stammverzeichnisses eines UNC-Server- oder Freigabenamens.
Ausnahmen
Das durch path
angegebene Verzeichnis ist schreibgeschützt.
Der Aufrufer verfügt nicht über die erforderliche Berechtigung.
.NET Framework und .NET Core-Versionen älter als 2.1: path
ist eine Zeichenfolge der Länge null, enthält nur Leerzeichen oder enthält ein oder mehrere ungültige Zeichen. Sie können Abfragen für ungültige Zeichen mithilfe der GetInvalidPathChars()-Methode ausführen.
path
ist null
.
Der angegebene Pfad und/oder Dateiname überschreiten die vom System definierte maximale Länge. Weitere Informationen finden Sie im Thema PathTooLongException.
Der angegebene Pfad wurde nicht gefunden.
path
weist ein ungültiges Format auf.
Nur .NET Framework: Der Aufrufer verfügt nicht über die erforderlichen Berechtigungen.
Beispiele
Im folgenden Beispiel wird veranschaulicht, wie Sie die GetParent -Methode verwenden, um das übergeordnete Verzeichnis eines vom Benutzer angegebenen Speicherorts "path" abzurufen. Der von der GetParent -Methode zurückgegebene Wert wird dann in der Konsole ausgegeben. Das Beispiel ist so konfiguriert, dass alle Fehler erfasst werden, die bei dieser Methode auftreten.
using namespace System;
class Class1
{
public:
void PrintFileSystemEntries( String^ path )
{
try
{
// Obtain the file system entries in the directory path.
array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path );
for ( int i = 0; i < directoryEntries->Length; i++ )
{
System::Console::WriteLine( directoryEntries[ i ] );
}
}
catch ( ArgumentNullException^ )
{
System::Console::WriteLine( "Path is a null reference." );
}
catch ( System::Security::SecurityException^ )
{
System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." );
}
catch ( ArgumentException^ )
{
System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." );
}
catch ( System::IO::DirectoryNotFoundException^ )
{
System::Console::WriteLine( "The path encapsulated in the \HelloServer' Directory object does not exist." );
}
}
void PrintFileSystemEntries( String^ path, String^ pattern )
{
try
{
// Obtain the file system entries in the directory
// path that match the pattern.
array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path, pattern );
for ( int i = 0; i < directoryEntries->Length; i++ )
{
System::Console::WriteLine( directoryEntries[ i ] );
}
}
catch ( ArgumentNullException^ )
{
System::Console::WriteLine( "Path is a null reference." );
}
catch ( System::Security::SecurityException^ )
{
System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." );
}
catch ( ArgumentException^ )
{
System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." );
}
catch ( System::IO::DirectoryNotFoundException^ )
{
System::Console::WriteLine( "The path encapsulated in the \HelloServer' Directory object does not exist." );
}
}
// Print out all logical drives on the system.
void GetLogicalDrives()
{
try
{
array<String^>^drives = System::IO::Directory::GetLogicalDrives();
for ( int i = 0; i < drives->Length; i++ )
{
System::Console::WriteLine( drives[ i ] );
}
}
catch ( System::IO::IOException^ )
{
System::Console::WriteLine( "An I/O error occurs." );
}
catch ( System::Security::SecurityException^ )
{
System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." );
}
}
void GetParent( String^ path )
{
try
{
System::IO::DirectoryInfo^ directoryInfo = System::IO::Directory::GetParent( path );
System::Console::WriteLine( directoryInfo->FullName );
}
catch ( ArgumentNullException^ )
{
System::Console::WriteLine( "Path is a null reference." );
}
catch ( ArgumentException^ )
{
System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, or \HelloServer' contains invalid characters." );
}
}
void Move( String^ sourcePath, String^ destinationPath )
{
try
{
System::IO::Directory::Move( sourcePath, destinationPath );
System::Console::WriteLine( "The directory move is complete." );
}
catch ( ArgumentNullException^ )
{
System::Console::WriteLine( "Path is a null reference." );
}
catch ( System::Security::SecurityException^ )
{
System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." );
}
catch ( ArgumentException^ )
{
System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." );
}
catch ( System::IO::IOException^ )
{
System::Console::WriteLine( "An attempt was made to move a \HelloServer' directory to a different \HelloServer' volume, or destDirName \HelloServer' already exists." );
}
}
};
int main()
{
Class1 * snippets = new Class1;
String^ path = System::IO::Directory::GetCurrentDirectory();
String^ filter = "*.exe";
snippets->PrintFileSystemEntries( path );
snippets->PrintFileSystemEntries( path, filter );
snippets->GetLogicalDrives();
snippets->GetParent( path );
snippets->Move( "C:\\proof", "C:\\Temp" );
return 0;
}
using System;
namespace GetFileSystemEntries
{
class Class1
{
static void Main(string[] args)
{
Class1 snippets = new Class1();
string path = System.IO.Directory.GetCurrentDirectory();
string filter = "*.exe";
snippets.PrintFileSystemEntries(path);
snippets.PrintFileSystemEntries(path, filter);
snippets.GetLogicalDrives();
snippets.GetParent(path);
snippets.Move("C:\\proof", "C:\\Temp");
}
void PrintFileSystemEntries(string path)
{
try
{
// Obtain the file system entries in the directory path.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(path);
foreach (string str in directoryEntries)
{
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the " +
"Directory object does not exist.");
}
}
void PrintFileSystemEntries(string path, string pattern)
{
try
{
// Obtain the file system entries in the directory
// path that match the pattern.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(path, pattern);
foreach (string str in directoryEntries)
{
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the " +
"Directory object does not exist.");
}
}
// Print out all logical drives on the system.
void GetLogicalDrives()
{
try
{
string[] drives = System.IO.Directory.GetLogicalDrives();
foreach (string str in drives)
{
System.Console.WriteLine(str);
}
}
catch (System.IO.IOException)
{
System.Console.WriteLine("An I/O error occurs.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
}
void GetParent(string path)
{
try
{
System.IO.DirectoryInfo directoryInfo =
System.IO.Directory.GetParent(path);
System.Console.WriteLine(directoryInfo.FullName);
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, or " +
"contains invalid characters.");
}
}
void Move(string sourcePath, string destinationPath)
{
try
{
System.IO.Directory.Move(sourcePath, destinationPath);
System.Console.WriteLine("The directory move is complete.");
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.IOException)
{
System.Console.WriteLine("An attempt was made to move a " +
"directory to a different " +
"volume, or destDirName " +
"already exists.");
}
}
}
}
open System
open System.IO
open System.Security
let printFileSystemEntries path =
try
// Obtain the file system entries in the directory path.
let directoryEntries = Directory.GetFileSystemEntries path
for str in directoryEntries do
printfn $"{str}"
with
| :? ArgumentNullException ->
printfn "Path is a null reference."
| :? SecurityException ->
printfn $"The caller does not have the required permission."
| :? ArgumentException ->
printfn $"Path is an empty string, contains only white spaces, or contains invalid characters."
| :? DirectoryNotFoundException ->
printfn $"The path encapsulated in the Directory object does not exist."
let printFileSystemEntriesPattern path pattern =
try
// Obtain the file system entries in the directory
// path that match the pattern.
let directoryEntries = Directory.GetFileSystemEntries(path, pattern)
for str in directoryEntries do
printfn $"{str}"
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? SecurityException -> printfn "The caller does not have the required permission."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
| :? DirectoryNotFoundException -> printfn "The path encapsulated in the Directory object does not exist."
// Print out all logical drives on the system.
let getLogicalDrives () =
try
let drives = Directory.GetLogicalDrives()
for str in drives do
printfn $"{str}"
with
| :? IOException -> printfn "An I/O error occurs."
| :? SecurityException -> printfn "The caller does not have the required permission."
let getParent path =
try
let directoryInfo = Directory.GetParent path
printfn $"{directoryInfo.FullName}"
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
let move sourcePath destinationPath =
try
Directory.Move(sourcePath, destinationPath)
printfn "The directory move is complete."
with
| :? ArgumentNullException -> printfn "Path is a null reference."
| :? SecurityException -> printfn "The caller does not have the required permission."
| :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
| :? IOException -> printfn "An attempt was made to move a directory to a different volume, or destDirName already exists."
let path = Directory.GetCurrentDirectory()
let filter = "*.exe"
printFileSystemEntries path
printFileSystemEntriesPattern path filter
getLogicalDrives ()
getParent path
move "C:\\proof" "C:\\Temp"
Option Explicit On
Option Strict On
Namespace GetFileSystemEntries
Class Class1
Overloads Shared Sub Main(ByVal args() As String)
Dim snippets As New Class1()
Dim path As String = System.IO.Directory.GetCurrentDirectory()
Dim filter As String = "*.exe"
snippets.PrintFileSystemEntries(path)
snippets.PrintFileSystemEntries(path, filter)
snippets.GetLogicalDrives()
snippets.GetParent(path)
snippets.Move("C:\proof", "C:\Temp")
End Sub
Sub PrintFileSystemEntries(ByVal path As String)
Try
' Obtain the file system entries in the directory path.
Dim directoryEntries As String()
directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
Dim str As String
For Each str In directoryEntries
System.Console.WriteLine(str)
Next str
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.DirectoryNotFoundException
System.Console.WriteLine("The path encapsulated in the " + _
"Directory object does not exist.")
End Try
End Sub
Sub PrintFileSystemEntries(ByVal path As String, _
ByVal pattern As String)
Try
' Obtain the file system entries in the directory
' path that match the pattern.
Dim directoryEntries As String()
directoryEntries = _
System.IO.Directory.GetFileSystemEntries(path, pattern)
Dim str As String
For Each str In directoryEntries
System.Console.WriteLine(str)
Next str
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.DirectoryNotFoundException
System.Console.WriteLine("The path encapsulated in the " + _
"Directory object does not exist.")
End Try
End Sub
' Print out all logical drives on the system.
Sub GetLogicalDrives()
Try
Dim drives As String()
drives = System.IO.Directory.GetLogicalDrives()
Dim str As String
For Each str In drives
System.Console.WriteLine(str)
Next str
Catch exp As System.IO.IOException
System.Console.WriteLine("An I/O error occurs.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
End Try
End Sub
Sub GetParent(ByVal path As String)
Try
Dim directoryInfo As System.IO.DirectoryInfo
directoryInfo = System.IO.Directory.GetParent(path)
System.Console.WriteLine(directoryInfo.FullName)
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, or " + _
"contains invalid characters.")
End Try
End Sub
Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
Try
System.IO.Directory.Move(sourcePath, destinationPath)
System.Console.WriteLine("The directory move is complete.")
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.IOException
System.Console.WriteLine("An attempt was made to move a " + _
"directory to a different " + _
"volume, or destDirName " + _
"already exists.")
End Try
End Sub
End Class
End Namespace
Hinweise
Der path
Parameter kann relative oder absolute Pfadinformationen angeben. Relative Pfadinformationen werden relativ zum aktuellen Arbeitsverzeichnis interpretiert. Informationen zum Abrufen des aktuellen Arbeitsverzeichnisses finden Sie unter GetCurrentDirectory.
Nachfolgende Leerzeichen werden am Ende des path
Parameters entfernt, bevor das Verzeichnis abgerufen wird.
Die von dieser Methode zurückgegebene Zeichenfolge besteht aus allen Zeichen im Pfad bis zum letzten DirectorySeparatorChar oder AltDirectorySeparatorChar. Wenn Sie beispielsweise den Pfad "C:\Directory\SubDirectory\test.txt" an übergeben, GetParent wird "C:\Directory\SubDirectory" zurückgegeben. Durch Übergeben von "C:\Directory\SubDirectory" wird "C:\Directory" zurückgegeben. Wenn Sie jedoch "C:\Directory\SubDirectory\" übergeben, wird "C:\Directory\SubDirectory" zurückgegeben, da das endende Verzeichnistrennzeichen nach "Unterverzeichnis" liegt.
Die Groß-/Kleinschreibung des path
Parameters entspricht der des Dateisystems, in dem der Code ausgeführt wird. Beispielsweise wird bei NTFS (dem standardmäßigen Windows-Dateisystem) die Groß-/Kleinschreibung und bei Linux-Dateisystemen die Groß-/Kleinschreibung nicht beachtet.
Eine Liste der allgemeinen E/A-Aufgaben finden Sie unter Allgemeine E/A-Aufgaben.