Path.GetFullPath 方法

定义

重载

GetFullPath(String)

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

GetFullPath(String, String)

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

GetFullPath(String)

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()定义的无效字符。

- 或 -

系统无法检索绝对路径。

调用方没有所需的权限。

path 上声明的默认值为 null

仅.NET Framework:path包含不属于卷标识符的冒号 (“:” () ,例如“c:\”) 。

指定的路径和/或文件名超过了系统定义的最大长度。

示例

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

String^ fileName = "myfile.ext";
String^ path = "\\mydir\\";
String^ fullPath;
fullPath = Path::GetFullPath( path );
Console::WriteLine( "GetFullPath('{0}') returns '{1}'", path, fullPath );
fullPath = Path::GetFullPath( fileName );
Console::WriteLine( "GetFullPath('{0}') returns '{1}'", fileName, 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'
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 是当前目录,则对文件名(如 test.txt)调用GetFullPath将返回c:\temp\newdir\test.txt。 该文件不需要存在。

重要

如果 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

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

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 是完全限定的路径,则 该方法将传递给 pathGetFullPath(String) 方法并返回结果。

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

适用于