Directory.GetDirectoryRoot(String) Metoda

Definice

Vrátí informace o svazku, kořenové informace nebo obojí pro zadanou cestu.

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

Parametry

path
String

Cesta k souboru nebo adresáři.

Návraty

Řetězec obsahující informace o svazku, kořenové informace nebo obojí pro zadanou cestu.

Výjimky

Volající nemá požadované oprávnění.

Verze .NET Framework a .NET Core starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Můžete zadat dotaz na neplatné znaky pomocí GetInvalidPathChars().

path je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

Příklady

Následující příklad ukazuje, jak nastavit aktuální adresář a zobrazit kořen adresáře.

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

Poznámky

Tato metoda získá plně kvalifikovaný název pathcesty , jak je vrácen GetFullPath, a vrátí informace o kořenovém adresáři. Zadaná cesta není nutná k existenci.

Parametr path má povoleno zadat relativní nebo absolutní informace o cestě. Relativní informace o cestě se interpretují jako relativní vzhledem k aktuálnímu pracovnímu adresáři. Aktuální pracovní adresář získáte v tématu GetCurrentDirectory.

Citlivost na malá a velká písmena path parametru odpovídá systému souborů, na kterém je kód spuštěný. Například nerozlišuje malá a velká písmena v systému souborů NTFS (výchozí systém souborů Windows) a v systémech linuxových souborů rozlišují malá a velká písmena.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Platí pro

Viz také