Directory.GetDirectoryRoot(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回指定路径的卷信息、根信息或两者。
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
参数
- path
- String
文件或目录的路径。
返回
一个字符串,包含指定路径的卷信息、根信息或两者。
例外
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用 < a0/> 查询无效字符
path 是 null。
指定的路径、文件名或两者都超过了系统定义的最大长度。
示例
下面的示例演示如何设置当前目录并显示目录根目录。
// 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
注解
此方法获取由GetFullPath其返回的path完全限定路径名称,并返回根目录信息。 不存在指定的路径。
允许参数 path 指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。