DirectoryInfo.Parent Propriété

Définition

Obtient le répertoire parent d'un sous-répertoire spécifié.Gets the parent directory of a specified subdirectory.

public:
 property System::IO::DirectoryInfo ^ Parent { System::IO::DirectoryInfo ^ get(); };
public System.IO.DirectoryInfo Parent { get; }
public System.IO.DirectoryInfo? Parent { get; }
member this.Parent : System.IO.DirectoryInfo
Public ReadOnly Property Parent As DirectoryInfo

Valeur de propriété

DirectoryInfo

Le répertoire parent, ou null si le chemin d’accès est null ou si le chemin d’accès du fichier désigne une racine (telle que \ , C:\ ou \\server\share ).The parent directory, or null if the path is null or if the file path denotes a root (such as \, C:\, or \\server\share).

Exceptions

L'appelant n'a pas l'autorisation requise.The caller does not have the required permission.

Exemples

L’exemple suivant fait référence au répertoire parent d’un répertoire spécifié.The following example refers to the parent directory of a specified directory.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "TempDir" );
   
   // Create the directory only if it does not already exist.
   if (  !di->Exists )
      di->Create();

   
   // Create a subdirectory in the directory just created.
   DirectoryInfo^ dis = di->CreateSubdirectory( "SubDir" );
   
   // Get a reference to the parent directory of the subdirectory you just made.
   DirectoryInfo^ parentDir = dis->Parent;
   Console::WriteLine( "The parent directory of '{0}' is '{1}'", dis->Name, parentDir->Name );
   
   // Delete the parent directory.
   di->Delete( true );
}

using System;
using System.IO;

public class MoveToTest
{
    public static void Main()
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Get a reference to the parent directory of the subdirectory you just made.
        DirectoryInfo parentDir = dis.Parent;
        Console.WriteLine("The parent directory of '{0}' is '{1}'", dis.Name, parentDir.Name);

        // Delete the parent directory.
        di.Delete(true);
    }
}
Imports System.IO

Public Class MoveToTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")
        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
            di.Create()
        End If

        ' Create a subdirectory in the directory just created.
        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")

        ' Get a reference to the parent directory of the subdirectory you just made.
        Dim parentDir As DirectoryInfo = dis.Parent
        Console.WriteLine("The parent directory of '{0}' is '{1}'", dis.Name, parentDir.Name)

        ' Delete the parent directory.
        di.Delete(True)
    End Sub
End Class

Remarques

Important

Dans .NET Framework, Parent retourne un chemin d’accès relatif.In .NET Framework, Parent returns a relative path. Dans .NET Core, Parent retourne un chemin d’accès qualifié complet.In .NET Core, Parent returns a fully qualified path.

Pour garantir un comportement cohérent entre les versions et rendre votre intention explicite, récupérez la valeur de l’une des propriétés suivantes sur l' DirectoryInfo instance retournée par Parent .To ensure consistent behavior across versions and to make your intent explicit, retrieve the value of one of the following properties on the DirectoryInfo instance returned by Parent.

  • Name, qui retourne le nom simple du répertoire (tel que bin ).Name, which returns the simple name of the directory (such as bin).
  • FullName, qui retourne le chemin d’accès absolu du répertoire.FullName, which returns the absolute path of the directory.

Pour obtenir la liste des tâches d’e/s courantes, consultez tâches d’e/s courantes.For a list of common I/O tasks, see Common I/O Tasks.

S’applique à

Voir aussi