Directory.GetDirectoryRoot(String) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Retourneert de volumegegevens, hoofdinformatie of beide voor het opgegeven pad.
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
Parameters
- path
- String
Het pad van een bestand of map.
Retouren
Een tekenreeks die de volumegegevens, hoofdinformatie of beide voor het opgegeven pad bevat.
Uitzonderingen
De beller heeft niet de vereiste machtiging.
.NET Framework en .NET Core-versies ouder dan 2.1: path is een tekenreeks met lengte nul, bevat alleen witruimte of bevat een of meer ongeldige tekens. U kunt een query uitvoeren op ongeldige tekens met GetInvalidPathChars().
path is null.
Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.
Voorbeelden
In het volgende voorbeeld ziet u hoe u de huidige map instelt en de hoofdmap van de map weergeeft.
// 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
Opmerkingen
Met deze methode wordt de volledig gekwalificeerde padnaam opgehaald van path, zoals geretourneerd door GetFullPathen retourneert hoofdmapgegevens. Het opgegeven pad is niet vereist om te bestaan.
De path parameter mag relatieve of absolute padgegevens opgeven. Relatieve padinformatie wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap. Als u de huidige werkmap wilt ophalen, raadpleegt GetCurrentDirectoryu .
De hoofdlettergevoeligheid van de path parameter komt overeen met die van het bestandssysteem waarop de code wordt uitgevoerd. Het is bijvoorbeeld niet hoofdlettergevoelig voor NTFS (het standaardbestandssysteem Windows) en hoofdlettergevoelig op Linux-bestandssystemen.
Zie Algemene I/O-taken voor een lijst met algemene I/O-taken.