Path.GetFullPath 方法

定义

重载

名称 说明
GetFullPath(String)

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

GetFullPath(String, String)

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

GetFullPath(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 ^ GetFullPath(System::String ^ path);
public static string GetFullPath(string path);
static member GetFullPath : string -> string
Public Shared Function GetFullPath (path As String) As String

参数

path
String

要为其获取绝对路径信息的文件或目录。

返回

完全限定的位置 path,例如“C:\MyFile.txt”。

例外

path是一个零长度的字符串,仅包含Windows系统上的空格,或者包含GetInvalidPathChars()中定义的一个或多个无效字符。

-或-

系统无法检索绝对路径。

调用方没有所需的权限。

pathnull

仅限.NET框架:path包含非卷标识符的冒号(“:”)(例如,“c:\”)。

指定的路径、文件名或两者都超过了系统定义的最大长度。

示例

以下示例演示基于Windows桌面平台上的 GetFullPath 方法。

string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath;

fullPath = Path.GetFullPath(path1);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    path1, fullPath);

fullPath = Path.GetFullPath(fileName);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    fileName, fullPath);

fullPath = Path.GetFullPath(path2);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    path2, fullPath);

// Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'
Dim fileName As string = "myfile.ext"
Dim path1 As string = "mydir"
Dim path2 As string = "\mydir"
Dim fullPath As string

fullPath = Path.GetFullPath(path1)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    path1, fullPath)

fullPath = Path.GetFullPath(fileName)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    fileName, fullPath)

fullPath = Path.GetFullPath(path2)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    path2, fullPath)

' Output is based on your current directory, except
' in the last case, where it is based on the root drive
' GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
' GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
' GetFullPath('\mydir') returns 'C:\mydir'

注解

绝对路径包括在系统上查找文件或目录所需的所有信息。

不需要存在指定的 path 文件或目录。 例如,如果 c:\temp\newdir 是当前目录,则调用 GetFullPath 文件名(如 test.txt )将返回 c:\temp\newdir\test.txt。 该文件不存在。

Important

如果 path 为相对路径,则此重载返回一个完全限定的路径,该路径可以基于当前驱动器和当前目录。 应用程序执行时,当前驱动器和当前目录可以随时更改。 因此,无法提前确定此重载返回的路径。 若要返回确定性路径,请调用 GetFullPath(String, String) 重载。 还可以调用 IsPathFullyQualified 该方法来确定路径是完全限定还是相对路径,因此是否需要调用 GetFullPath

但是,如果 path 存在,调用方必须具有获取路径信息 path的权限。 请注意,与类的 Path 大多数成员不同,此方法访问文件系统。

此方法使用当前目录和当前卷信息完全限定 path。 如果仅在其中 path指定文件名, GetFullPath 则返回当前目录的完全限定路径。

如果传入短文件名,则会将其扩展为长文件名。

如果路径不包含任何重要字符,则它无效,除非它包含一个或多个“.”字符,后跟任意数量的空格;然后,它将分析为“.”或“..”。

.NET Core 1.1 及更高版本以及 .NET Framework 4.6.2 及更高版本也支持包含设备名称的路径,例如“\\?\C:\”。

有关 Windows 上的文件路径格式的详细信息,请参阅 Windows 系统上的文件路径格式。 有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

另请参阅

适用于

GetFullPath(String, 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 ^ GetFullPath(System::String ^ path, System::String ^ basePath);
public static string GetFullPath(string path, string basePath);
static member GetFullPath : string * string -> string
Public Shared Function GetFullPath (path As String, basePath As String) As String

参数

path
String

要连接到 basePath的相对路径。

basePath
String

完全限定路径的开头。

返回

绝对路径。

例外

pathbasePathnull.

basePath 不是完全限定的路径。

-或-

pathbasePath 包含一个或多个在 . 中 GetInvalidPathChars()定义的无效字符。

示例

以下示例定义一个变量, basePath用于表示应用程序的当前目录。 然后,它会将其传递给 GetFullPath 方法,以获取应用程序数据目录的完全限定路径。

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string basePath = Environment.CurrentDirectory;
        string relativePath = "./data/output.xml";
 
        // Unexpectedly change the current directory.
        Environment.CurrentDirectory = "C:/Users/Public/Documents/";
        
        string fullPath = Path.GetFullPath(relativePath, basePath);
        Console.WriteLine($"Current directory:\n   {Environment.CurrentDirectory}");
        Console.WriteLine($"Fully qualified path:\n   {fullPath}");
    }
}
// The example displays the following output:
//   Current directory:
//      C:\Users\Public\Documents
//   Fully qualified path:
//      C:\Utilities\data\output.xml
Imports System.IO

Module Program
    Public Sub Main()
        Dim basePath As String = Environment.CurrentDirectory
        Dim relativePath As String = "./data/output.xml"
 
        ' Unexpectedly change the current directory.
        Environment.CurrentDirectory = "C:/Users/Public/Documents/"
        
        Dim fullPath As String = Path.GetFullPath(relativePath, basePath)
        Console.WriteLine($"Current directory:\n   {Environment.CurrentDirectory}")
        Console.WriteLine($"Fully qualified path:\n   {fullPath}")
    End Sub
End Module
' The example displays the following output:
'   Current directory:
'      C:\Users\Public\Documents
'   Fully qualified path:
'      C:\Utilities\data\output.xml

注解

如果 path 为空路径,则该方法返回 basePath。 如果path为完全限定的路径,该方法将path传递给GetFullPath(String)该方法并返回结果。

使用此方法可在使用相对路径时基于指定的卷和根目录返回确定性路径。 使用预定义 basePath 文件而不是基于当前驱动器目录的预定义文件路径可防范当前驱动器和目录中意外更改导致的不需要的文件路径。

适用于