Directory.GetDirectoryRoot(String) Metoda

Definicja

Zwraca informacje o woluminie, informacje główne lub obie dla określonej ścieżki.

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

Ścieżka pliku lub katalogu.

Zwraca

Ciąg zawierający informacje o woluminie, informacje główne lub oba dla określonej ścieżki.

Wyjątki

Obiekt wywołujący nie posiada wymaganych uprawnień.

.NET Framework i .NET Core w wersjach starszych niż 2.1: path jest ciągiem o zerowej długości, zawiera tylko biały odstęp lub zawiera co najmniej jeden nieprawidłowy znak. Zapytania dotyczące nieprawidłowych znaków można wykonać za pomocą polecenia GetInvalidPathChars().

path to null.

Podana ścieżka, nazwa pliku lub obie przekraczają maksymalną długość zdefiniowaną przez system.

Przykłady

W poniższym przykładzie pokazano, jak ustawić bieżący katalog i wyświetlić katalog główny katalogu.

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

Uwagi

Ta metoda uzyskuje w pełni kwalifikowaną nazwę ścieżki , pathzwracaną przez GetFullPathmetodę i zwraca informacje o katalogu głównym. Określona ścieżka nie jest wymagana do istnienia.

Parametr path może określać informacje o ścieżce względnej lub bezwzględnej. Informacje o ścieżce względnej są interpretowane jako względne w stosunku do bieżącego katalogu roboczego. Aby uzyskać bieżący katalog roboczy, zobacz GetCurrentDirectory.

Ważność wielkości liter parametru path odpowiada wartości systemu plików, na którym działa kod. Na przykład jest rozróżniana wielkość liter w systemie plików NTFS (domyślny system plików systemu Windows) i rozróżniana wielkość liter w systemach plików systemu Linux.

Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.

Dotyczy

Zobacz też