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
属性

示例

下面的示例演示 类的一些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

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

方法

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)

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

适用于

另请参阅