Directory.GetDirectoryRoot(String) Méthode

Définition

Retourne les informations sur le volume, les informations sur la racine ou les deux, pour le chemin d’accès spécifié.

public:
 static System::String ^ GetDirectoryRoot(System::String ^ path);
public static string GetDirectoryRoot (string path);
static member GetDirectoryRoot : string -> string
Public Shared Function GetDirectoryRoot (path As String) As String

Paramètres

path
String

Chemin d’accès d’un fichier ou d’un répertoire.

Retours

Chaîne qui contient les informations sur le volume, les informations sur la racine ou les deux, pour le chemin d’accès spécifié.

Exceptions

L'appelant n'a pas l'autorisation requise.

.NET Framework et .NET Core versions antérieures à 2.1 : path est une chaîne de longueur nulle, contient uniquement des espaces blancs ou contient un ou plusieurs caractères non valides. Vous pouvez rechercher les caractères non valides à l’aide de GetInvalidPathChars().

path a la valeur null.

Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.

Exemples

L’exemple suivant montre comment définir le répertoire actif et afficher la racine du répertoire.

// This sample shows how to set the current directory and how to determine
// the root directory.
using namespace System;
using namespace System::IO;
int main()
{
   
   // Create string for a directory. This value should be an existing directory
   // or the sample will throw a DirectoryNotFoundException.
   String^ dir = "C:\\test";
   try
   {
      
      //Set the current directory.
      Directory::SetCurrentDirectory( dir );
   }
   catch ( DirectoryNotFoundException^ e ) 
   {
      Console::WriteLine( "The specified directory does not exist. {0}", e );
   }

   
   // Print to console the results.
   Console::WriteLine( "Root directory: {0}", Directory::GetDirectoryRoot( dir ) );
   Console::WriteLine( "Current directory: {0}", Directory::GetCurrentDirectory() );
}

// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
// This sample shows how to set the current directory and how to determine
// the root directory.
using System;
using System.IO;

namespace IOSamples
{
  public class DirectoryRoot
  {
    public static void Main()
    {
    // Create string for a directory. This value should be an existing directory
    // or the sample will throw a DirectoryNotFoundException.
      string dir = @"C:\test";		
      try
      {
          //Set the current directory.
          Directory.SetCurrentDirectory(dir);
      }
      catch (DirectoryNotFoundException e)
      {
          Console.WriteLine("The specified directory does not exist. {0}", e);
      }
    // Print to console the results.
      Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir));
      Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory());
    }
  }
}
// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
// This sample shows how to set the current directory and how to determine
// the root directory.
open System.IO

// Create string for a directory. This value should be an existing directory
// or the sample will throw a DirectoryNotFoundException.
let dir = @"C:\test"
try
    //Set the current directory.
    Directory.SetCurrentDirectory dir
with :? DirectoryNotFoundException as e ->
    printfn $"The specified directory does not exist. {e}"
    
// Print to console the results.
printfn $"Root directory: {Directory.GetDirectoryRoot dir}"
printfn $"Current directory: {Directory.GetCurrentDirectory()}"
// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
' This sample shows how to set the current directory and how to determine
' the root directory.
Imports System.IO

Public Class DirectoryRoot
   
   Public Shared Sub Main()
      ' Create string for a directory. This value should be an existing directory
      ' or the sample will throw a DirectoryNotFoundException.
      Dim dir As String = "C:\test"
      Try
         'Set the current directory.
         Directory.SetCurrentDirectory(dir)
      Catch e As DirectoryNotFoundException
         Console.WriteLine("The specified directory does not exist. {0}", e)
      End Try
      ' Print to console the results.
      Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir))
      Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory())
   End Sub
End Class
' The output of this sample depends on what value you assign to the variable dir.
' If the directory c:\test exists, the output for this sample is:
' Root directory: C:\
' Current directory: C:\test

Remarques

Cette méthode obtient le nom de chemin d’accès complet de path, tel que retourné par GetFullPath, et retourne les informations du répertoire racine. Il n’est pas nécessaire que le chemin d’accès spécifié existe.

Le path paramètre est autorisé à spécifier des informations relatives ou absolues sur le chemin d’accès. Les informations relatives au chemin d’accès sont interprétées comme relatives au répertoire de travail actuel. Pour obtenir le répertoire de travail actuel, consultez GetCurrentDirectory.

Le respect de la casse du path paramètre correspond à celui du système de fichiers sur lequel le code s’exécute. Par exemple, il ne respecte pas la casse sur NTFS (le système de fichiers Windows par défaut) et respecte la casse sur les systèmes de fichiers Linux.

Pour obtenir la liste des tâches d’E/S courantes, consultez Tâches d’E/S courantes.

S’applique à

Voir aussi