Path 类

定义

对包含文件或目录路径信息的 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
属性

示例

以下示例演示了 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

注解

路径是提供文件或目录位置的字符串。 路径不一定指向磁盘上的位置;例如,路径可能映射到内存中或设备上的位置。 路径的确切格式由当前平台确定。 例如,在某些系统上,路径可以从驱动器或卷号开始,而此元素在其他系统中不存在。 在某些系统上,文件路径可以包含扩展,这些扩展指示存储在文件中的信息类型。 文件扩展名的格式依赖于平台;例如,某些系统将扩展限制为三个字符 (,例如在较小的闪存存储和较旧版本的 ISO 9660 上使用的 ISO 9660) ,而其他系统则不这样做。 当前平台还确定用于分隔路径元素的字符集,以及指定路径时无法使用的字符集。 由于这些差异,类的 Path 字段以及类的某些成员 Path 的确切行为依赖于平台。

路径可以包含绝对或相对位置信息。 绝对路径完全指定位置:无论当前位置如何,都可以唯一标识文件或目录。 相对路径指定部分位置:定位使用相对路径指定的文件时,当前位置用作起点。 若要确定当前目录,请调用 Directory.GetCurrentDirectory

.NET Core 1.1 及更高版本和.NET Framework 4.6.2 及更高版本还支持访问设备名称为“\\??\C:\“.

有关Windows上的文件路径格式的详细信息,请参阅Windows系统上的文件格式

类的 Path 大多数成员不会与文件系统交互,并且不验证路径字符串指定的文件是否存在。 Path 修改路径字符串的类成员(例如 ChangeExtension)对文件系统中的文件名称没有影响。

某些 Path 成员会验证指定路径字符串的内容,如果 ArgumentException 字符串包含路径字符串中无效的字符,如方法 GetInvalidPathChars 返回的字符中定义。 例如,在基于Windows的桌面平台上,无效的路径字符可能包括引号 (“) 、小于 (<), greater than (>) 、管道 (|) 、backspace (\b) 、null (\0) 和 Unicode 字符 16 到 18 和 20 到 25。 此验证行为因 .NET 版本而异:

  • 在低于 2.1 的 .NET Framework 和 .NET Core 版本上:如果成员检测到路径字符无效,则会引发ArgumentException路径作为参数的所有Path成员。

  • 在 .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# 中的“MyDirMySubdir\\”或Visual Basic中的“MyDirMySubDir\”。

  • C# 中的“\\\\MyServerMyShare\\”或Visual Basic中的“\\MyServerMyShare”。\

由于所有这些操作都在字符串上执行,因此无法验证结果在所有方案中是否有效。 例如,该方法 GetExtension 分析传递给它的字符串,并从该字符串返回扩展。 但是,这并不意味着磁盘上存在扩展名的文件。

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

字段

AltDirectorySeparatorChar

提供平台特定的替换字符,该替换字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。

DirectorySeparatorChar

提供平台特定的字符,该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。

InvalidPathChars
已过时。
已过时。
已过时。

提供平台特定的字符数组,这些字符不能在传递到 Path 类的成员的路径字符串自变量中指定。

PathSeparator

用于在环境变量中分隔路径字符串的平台特定的分隔符。

VolumeSeparatorChar

提供平台特定的卷分隔符。

方法

ChangeExtension(String, String)

更改路径字符串的扩展名。

Combine(String, String)

将两个字符串组合成一个路径。

Combine(String, String, String)

将三个字符串组合成一个路径。

Combine(String, String, String, String)

将四个字符串组合成一个路径。

Combine(String[])

将字符串数组组合成一个路径。

EndsInDirectorySeparator(ReadOnlySpan<Char>)

返回一个值,该值指示指定为只读范围的路径是否以目录分隔符结尾。

EndsInDirectorySeparator(String)

返回一个值,该值指示指定的路径是否以目录分隔符结尾。

Exists(String)

确定指定的文件或目录是否存在。

GetDirectoryName(ReadOnlySpan<Char>)

返回由字符范围表示的指定路径的目录信息。

GetDirectoryName(String)

返回指定路径的目录信息。

GetExtension(ReadOnlySpan<Char>)

返回由只读字符范围表示的文件路径的扩展名。

GetExtension(String)

返回指定路径字符串的扩展名(包括句点“.”)。

GetFileName(ReadOnlySpan<Char>)

返回由只读字符范围表示的文件路径的文件名和扩展名。

GetFileName(String)

返回指定路径字符串的文件名和扩展名。

GetFileNameWithoutExtension(ReadOnlySpan<Char>)

返回文件名,该文件名不带由只读字符范围表示的文件路径扩展名。

GetFileNameWithoutExtension(String)

返回不具有扩展名的指定路径字符串的文件名。

GetFullPath(String)

返回指定路径字符串的绝对路径。

GetFullPath(String, String)

从完全限定的基路径和相对路径返回绝对路径。

GetInvalidFileNameChars()

获取包含不允许在文件名中使用的字符的数组。

GetInvalidPathChars()

获取包含不允许在路径名中使用的字符的数组。

GetPathRoot(ReadOnlySpan<Char>)

从指定字符范围包含的路径中获取根目录信息。

GetPathRoot(String)

从指定字符串包含的路径中获取根目录信息。

GetRandomFileName()

返回随机文件夹名或文件名。

GetRelativePath(String, String)

返回从一个路径到另一个路径的相对路径。

GetTempFileName()

在磁盘上创建一个唯一命名的零字节临时文件,并返回该文件的完整路径。

GetTempPath()

返回当前用户的临时文件夹的路径。

HasExtension(ReadOnlySpan<Char>)

确定指定字符范围表示的路径是否包含文件扩展名。

HasExtension(String)

确定路径是否包括文件扩展名。

IsPathFullyQualified(ReadOnlySpan<Char>)

返回一个值,该值指示由指定字符范围表示的文件路径是否固定到特定驱动器或 UNC 路径。

IsPathFullyQualified(String)

返回一个值,该值指示指定的文件路径是否固定到特定驱动器或 UNC 路径。

IsPathRooted(ReadOnlySpan<Char>)

返回一个值,该值指示表示文件路径的指定字符范围是否包含根。

IsPathRooted(String)

返回一个值,该值指示指定的路径字符串是否包含根。

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

将两个路径组件连接到一个路径中。

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

将三个路径组件连接到一个路径中。

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

将四个路径组件连接到一个路径中。

Join(String, String)

将两个路径连接到一个路径中。

Join(String, String, String)

将三个路径连接到一个路径中。

Join(String, String, String, String)

将四个路径连接到一个路径中。

Join(String[])

将路径数组连接到一个路径中。

TrimEndingDirectorySeparator(ReadOnlySpan<Char>)

剪裁一个超出指定路径根目录的尾随目录分隔符。

TrimEndingDirectorySeparator(String)

剪裁一个超出指定路径根目录的尾随目录分隔符。

TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32)

尝试将三个路径组件连接到单个预分配字符范围,并返回一个值,该值指示操作是否成功。

TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32)

尝试将两个路径组件连接到一个预分配的字符范围,并返回一个值,该值指示操作是否成功。

适用于

另请参阅