System.IO.Path.GetFullPath 方法

方法描述

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

语法定义(C# System.IO.Path.GetFullPath 方法 的用法)

public static string GetFullPath(
	string path
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
path System-String 要为其获取绝对路径信息的文件或目录。
返回值 System.String path 的完全限定的位置,例如“C:\MyFile.txt”。

提示和注释

.NET Framework 不支持通过由设备名称构成的路径(如“\\)直接访问物理磁盘。 \PHYSICALDRIVE0 ".

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

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

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

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

如果您以短文件名传递,则它将扩展为长文件名。

如果路径不包含有效字符,除非它包含一个或多个"."字符,字符后面带有任意数量的空格,否则它是无效的;然后它将被分析为任何一个"."或".."。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.Path.GetFullPath 方法例子

下面的代码示例演示基于 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'

异常

异常 异常描述
ArgumentException
  • path 是一个零长度字符串,仅包含空白或者包含 GetInvalidPathChars 中已定义一个或多个无效字符。
  • 系统未能检索绝对路径。
SecurityException 调用方没有所需的权限。
ArgumentNullException path 为 null。
NotSupportedException path 包含一个冒号(“:”),此冒号不是卷标识符(如,“c:\”)的一部分。
PathTooLongException 指定的路径、文件名或者两者都超出了系统定义的最大长度。 例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。