Directory.GetDirectoryRoot(String) Methode

Definition

Gibt für den angegebenen Pfad die Informationen über Volume, Stammverzeichnis oder beides zurück.

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

Parameter

path
String

Der Pfad einer Datei oder eines Verzeichnisses.

Gibt zurück

Eine Zeichenfolge, die für den angegebenen Pfad die Informationen über Volume, Stammverzeichnis oder beides enthält.

Ausnahmen

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 ein oder mehrere ungültige Zeichen. Mit GetInvalidPathChars() können Sie Abfragen für ungültige Zeichen ausführen.

path ist null.

Der angegebene Pfad und/oder Dateiname überschreiten die vom System definierte maximale Länge.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie das aktuelle Verzeichnis festgelegt und das Verzeichnisstamm angezeigt wird.

// 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

Hinweise

Diese Methode ruft den vollqualifizierten Pfadnamen von pathab, der von zurückgegeben wird GetFullPath, und gibt Stammverzeichnisinformationen zurück. Der angegebene Pfad muss nicht vorhanden sein.

Der path Parameter darf relative oder absolute Pfadinformationen angeben. Relative Pfadinformationen werden relativ zum aktuellen Arbeitsverzeichnis interpretiert. Informationen zum Abrufen des aktuellen Arbeitsverzeichnisses finden Sie unter GetCurrentDirectory.

Die Groß-/Kleinschreibung des path Parameters entspricht der des Dateisystems, auf dem der Code ausgeführt wird. Beispielsweise wird bei NTFS (dem Windows-Standarddateisystem) die Groß-/Kleinschreibung nicht beachtet und bei Linux-Dateisystemen die Groß-/Kleinschreibung beachtet.

Eine Liste allgemeiner E/A-Aufgaben finden Sie unter Allgemeine E/A-Aufgaben.

Gilt für:

Weitere Informationen