Directory.GetDirectoryRoot(String) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Returnerar volyminformation, rotinformation eller båda för den angivna sökvägen.
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
Parametrar
- path
- String
Sökvägen till en fil eller katalog.
Returer
En sträng som innehåller volyminformation, rotinformation eller båda för den angivna sökvägen.
Undantag
Anroparen har inte den behörighet som krävs.
.NET Framework- och .NET Core-versioner som är äldre än 2.1: path är en sträng med noll längd, innehåller endast tomt utrymme eller innehåller ett eller flera ogiltiga tecken. Du kan fråga efter ogiltiga tecken med GetInvalidPathChars().
path är null.
Den angivna sökvägen, filnamnet eller båda överskrider den systemdefinierade maximala längden.
Exempel
I följande exempel visas hur du anger den aktuella katalogen och visar katalogroten.
// 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
Kommentarer
Den här metoden hämtar det fullständigt kvalificerade sökvägsnamnet path, som returneras av GetFullPath, och returnerar rotkataloginformation. Den angivna sökvägen krävs inte för att finnas.
Parametern path tillåts ange relativ eller absolut sökvägsinformation. Relativ sökvägsinformation tolkas som relativ till den aktuella arbetskatalogen. Information om hur du hämtar den aktuella arbetskatalogen finns i GetCurrentDirectory.
Parameterns path skiftlägeskänslighet motsvarar det filsystem där koden körs. Det är till exempel skiftlägeskänsligt på NTFS (standard Windows filsystem) och skiftlägeskänsligt på Linux-filsystem.
En lista över vanliga I/O-uppgifter finns i Vanliga I/O-uppgifter.