Path 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
对包含文件或目录路径信息的 String 实例执行操作。 这些操作是以跨平台的方式执行的。
public ref class Path abstract sealed
public ref class Path sealed
public static class Path
public sealed class Path
[System.Runtime.InteropServices.ComVisible(true)]
public static class Path
type Path = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type Path = class
Public Class Path
Public NotInheritable Class Path
- 继承
-
Path
- 属性
示例
下面的示例演示 类的一些main成员Path
。
using namespace System;
using namespace System::IO;
int main()
{
String^ path1 = "c:\\temp\\MyTest.txt";
String^ path2 = "c:\\temp\\MyTest";
String^ path3 = "temp";
if ( Path::HasExtension( path1 ) )
{
Console::WriteLine( "{0} has an extension.", path1 );
}
if ( !Path::HasExtension( path2 ) )
{
Console::WriteLine( "{0} has no extension.", path2 );
}
if ( !Path::IsPathRooted( path3 ) )
{
Console::WriteLine( "The string {0} contains no root information.", path3 );
}
Console::WriteLine( "The full path of {0} is {1}.", path3, Path::GetFullPath( path3 ) );
Console::WriteLine( "{0} is the location for temporary files.", Path::GetTempPath() );
Console::WriteLine( "{0} is a file available for use.", Path::GetTempFileName() );
Console::WriteLine( "\r\nThe set of invalid characters in a path is:" );
Console::WriteLine( "(Note that the wildcard characters '*' and '?' are not invalid.):" );
Collections::IEnumerator^ myEnum = Path::InvalidPathChars->GetEnumerator();
while ( myEnum->MoveNext() )
{
Char c = *safe_cast<Char^>(myEnum->Current);
Console::WriteLine( c );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
string path1 = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp\MyTest";
string path3 = @"temp";
if (Path.HasExtension(path1))
{
Console.WriteLine("{0} has an extension.", path1);
}
if (!Path.HasExtension(path2))
{
Console.WriteLine("{0} has no extension.", path2);
}
if (!Path.IsPathRooted(path3))
{
Console.WriteLine("The string {0} contains no root information.", path3);
}
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3));
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath());
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName());
/* This code produces output similar to the following:
* c:\temp\MyTest.txt has an extension.
* c:\temp\MyTest has no extension.
* The string temp contains no root information.
* The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
*/
}
}
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path1 As String = "c:\temp\MyTest.txt"
Dim path2 As String = "c:\temp\MyTest"
Dim path3 As String = "temp"
If Path.HasExtension(path1) Then
Console.WriteLine("{0} has an extension.", path1)
End If
If Path.HasExtension(path2) = False Then
Console.WriteLine("{0} has no extension.", path2)
End If
If Path.IsPathRooted(path3) = False Then
Console.WriteLine("The string {0} contains no root information.", path3)
End If
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3))
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath())
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName())
' This code produces output similar to the following:
' c:\temp\MyTest.txt has an extension.
' c:\temp\MyTest has no extension.
' The string temp contains no root information.
' The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
' D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
' D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
End Sub
End Class
注解
路径是提供文件或目录位置的字符串。 路径不一定指向磁盘上的某个位置;例如,路径可能映射到内存中或设备上的某个位置。 路径的确切格式由当前平台确定。 例如,在某些系统上,路径可以以驱动器或卷号开头,而此元素在其他系统中不存在。 在某些系统上,文件路径可以包含扩展,这些扩展指示存储在文件中的信息类型。 文件扩展名的格式依赖于平台;例如,某些系统将扩展限制为三个字符 (例如 FAT16 通常用于较小的闪存存储和用于光学媒体) 的旧版 ISO 9660,而其他系统则不这样做。 当前平台还确定用于分隔路径元素的字符集,以及指定路径时不能使用的字符集。 由于这些差异,类的 Path
字段以及类的某些成员的 Path
确切行为依赖于平台。
路径可以包含绝对位置信息或相对位置信息。 绝对路径完全指定位置:无论当前位置如何,都可以唯一标识文件或目录。 相对路径指定部分位置:定位使用相对路径指定的文件时,当前位置用作起点。 若要确定当前目录,请调用 Directory.GetCurrentDirectory。
.NET Core 1.1 及更高版本以及.NET Framework 4.6.2 及更高版本也支持访问作为设备名称的文件系统对象,例如“\\?\C:\”。
有关 Windows 上的文件路径格式的详细信息,请参阅 Windows 系统上的文件路径格式。
类的 Path
大多数成员不与文件系统交互,也不验证路径字符串指定的文件是否存在。 Path
修改路径字符串(如 ) ChangeExtension的类成员对文件系统中文件的名称没有影响。
某些Path
成员会验证指定路径字符串的内容,如果字符串包含路径字符串中无效的字符(如 方法返回GetInvalidPathChars的字符中定义),则会引发 ArgumentException 。 例如,在基于 Windows 的桌面平台上,无效的路径字符可能包括引号 (“) 、小于 (<) 、大于 (>) 、管道 (|) 、backspace (\b) 、null (\0) 以及 Unicode 字符 16 到 18 以及 20 到 25。 此验证行为因 .NET 版本而异:
在早于 2.1 的 .NET Framework 和 .NET Core 版本上:所有
Path
将路径作为参数的成员如果检测到无效的路径字符,则会引发 ArgumentException 。在 .NET Core 2.1 及更高版本上: GetFullPath 是当字符串包含无效路径字符时引发 的唯一 ArgumentException 成员。
使用 类的成员 Path
可以快速轻松地执行常见操作,例如确定文件扩展名是否是路径的一部分,以及将两个字符串合并为一个路径名称。
类的所有成员都是静态的 Path
,因此无需路径实例即可调用。
注意
在接受路径作为输入字符串的成员中,该路径的格式必须正确,否则将引发异常。 例如,如果路径是完全限定的,但以空格开头,则不会在 类的方法中剪裁该路径。 因此,路径格式不正确,并引发异常。 同样,路径或路径组合不能完全限定两次。 例如,“c:\temp c:\windows”在大多数情况下也会引发异常。 使用接受路径字符串的方法时,请确保路径格式良好。
在接受路径的成员中,路径可以引用文件或仅引用目录。 指定的路径还可以引用服务器和共享名称的相对路径或通用命名约定 (UNC) 路径。 例如,以下所有路径都是可接受的路径:
C# 中的“c:\\MyDir\\MyFile.txt”或 Visual Basic 中的“c:\MyDir\MyFile.txt”。
C# 中的“c:\\MyDir”或 Visual Basic 中的“c:\MyDir”。
C# 中的“MyDir\\MySubdir”或 Visual Basic 中的“MyDir\MySubDir”。
C# 中的“\\\\MyServer\\MyShare”或 Visual Basic 中的“\\MyServer\MyShare”。
由于所有这些操作都是在字符串上执行的,因此无法验证结果是否在所有方案中都有效。 例如, GetExtension 方法分析传递给它的字符串,并从该字符串返回扩展。 但是,这并不意味着磁盘上存在具有该扩展名的文件。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
字段
AltDirectorySeparatorChar |
提供平台特定的替换字符,该替换字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。 |
DirectorySeparatorChar |
提供平台特定的字符,该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。 |
InvalidPathChars |
已过时.
已过时.
提供平台特定的字符数组,这些字符不能在传递到 Path 类的成员的路径字符串自变量中指定。 |
PathSeparator |
用于在环境变量中分隔路径字符串的平台特定的分隔符。 |
VolumeSeparatorChar |
提供平台特定的卷分隔符。 |
方法
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈