Path.GetPathRoot 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| GetPathRoot(String) |
从指定字符串中包含的路径获取根目录信息。 |
| GetPathRoot(ReadOnlySpan<Char>) |
从指定字符范围中包含的路径获取根目录信息。 |
GetPathRoot(String)
- Source:
- Path.Unix.cs
- Source:
- Path.Unix.cs
- Source:
- Path.Unix.cs
- Source:
- Path.Unix.cs
- Source:
- Path.Unix.cs
从指定字符串中包含的路径获取根目录信息。
public:
static System::String ^ GetPathRoot(System::String ^ path);
public static string GetPathRoot(string path);
public static string? GetPathRoot(string? path);
static member GetPathRoot : string -> string
Public Shared Function GetPathRoot (path As String) As String
参数
- path
- String
包含从中获取根目录信息的路径的字符串。
返回
如果根目录已根目录,则为根目录 path 。
-或-
-或-
null 如果 path 为 null 或实际上为空,则为空。
例外
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 包含在其中 GetInvalidPathChars()定义的一个或多个无效字符。
-或-
仅限 .NET Framework: Empty 已传递给 path.
示例
下面的示例演示了该方法的使用 GetPathRoot 。
string path = @"\mydir\";
string fileName = "myfile.ext";
string fullPath = @"C:\mydir\myfile.ext";
string pathRoot;
pathRoot = Path.GetPathRoot(path);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
path, pathRoot);
pathRoot = Path.GetPathRoot(fileName);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
fileName, pathRoot);
pathRoot = Path.GetPathRoot(fullPath);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
fullPath, pathRoot);
// This code produces output similar to the following:
//
// GetPathRoot('\mydir\') returns '\'
// GetPathRoot('myfile.ext') returns ''
// GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'
Dim pathname As String = "\mydir\"
Dim fileName As String = "myfile.ext"
Dim fullPath As String = "C:\mydir\myfile.ext"
Dim pathnameRoot As String
pathnameRoot = Path.GetPathRoot(pathname)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", pathname, pathnameRoot)
pathnameRoot = Path.GetPathRoot(fileName)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fileName, pathnameRoot)
pathnameRoot = Path.GetPathRoot(fullPath)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fullPath, pathnameRoot)
' This code produces output similar to the following:
'
' GetPathRoot('\mydir\') returns '\'
' GetPathRoot('myfile.ext') returns ''
' GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'
注解
此方法不验证路径或文件是否存在。
此方法将规范化目录分隔符。
如果:
- 在 Windows 中,调用
IsEmpty此字符串将true返回,或者其所有字符都是空格(' ')。 - 在 Unix 中,调用 IsNullOrEmpty 此字符串将
true返回 。
此方法返回的字符串的可能模式如下所示:
null(path为 null 或空字符串)。空字符串(
path指定当前驱动器或卷上的相对路径)。“/” (Unix:
path指定当前驱动器上的绝对路径)。“X:”(Windows:
path在驱动器上指定相对路径,其中 X 表示驱动器或卷号)。“X:\” (Windows:
path指定给定驱动器上的绝对路径)。“\\ComputerName\SharedFolder”(Windows:UNC 路径)。
“\\?\C:”(Windows:.NET 版本和 .NET Framework 4.6.2 及更高版本中支持的 DOS 设备路径)。
有关 Windows 上的文件路径的详细信息,请参阅 Windows 系统上的文件路径格式。 有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
GetPathRoot(ReadOnlySpan<Char>)
- Source:
- Path.Unix.cs
- Source:
- Path.Unix.cs
- Source:
- Path.Unix.cs
- Source:
- Path.Unix.cs
- Source:
- Path.Unix.cs
从指定字符范围中包含的路径获取根目录信息。
public:
static ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path);
static member GetPathRoot : ReadOnlySpan<char> -> ReadOnlySpan<char>
Public Shared Function GetPathRoot (path As ReadOnlySpan(Of Char)) As ReadOnlySpan(Of Char)
参数
- path
- ReadOnlySpan<Char>
包含从中获取根目录信息的路径的字符的只读范围。
返回
包含根目录的 path字符的只读范围。
注解
此方法不验证路径或文件是否存在。
与字符串重载不同,此方法不会规范化目录分隔符。
如果为“有效为空”,则 ReadOnlySpan<System.Char> 为:
- 在 Windows 中,调用 ReadOnlySpan<T>.IsEmpty 此范围字符将
true返回,或者其所有字符都是空格(' ')。 - 在 Unix 中,调用 ReadOnlySpan<T>.IsEmpty 此范围字符将
true返回 。
此方法返回的只读字符范围的可能模式如下所示:
ReadOnlySpan<T>.Empty (
path指定当前驱动器或卷上的相对路径)。“/” (Unix:
path指定当前驱动器上的绝对路径)。“X:”(Windows:
path在驱动器上指定相对路径,其中 X 表示驱动器或卷号)。“X:\” (Windows:
path指定给定驱动器上的绝对路径)。“\\ComputerName\SharedFolder”(Windows:UNC 路径)。
“\\?\C:”(Windows:.NET 版本和 .NET Framework 4.6.2 及更高版本中支持的 DOS 设备路径)。
有关 Windows 上的文件路径的详细信息,请参阅 Windows 系统上的文件路径格式。 有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。