Path.GetFullPath 方法

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

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Function GetFullPath ( _
    path As String _
) As String
用法
Dim path As String
Dim returnValue As String

returnValue = Path.GetFullPath(path)
public static string GetFullPath (
    string path
)
public:
static String^ GetFullPath (
    String^ path
)
public static String GetFullPath (
    String path
)
public static function GetFullPath (
    path : String
) : String

参数

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

返回值

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

异常

异常类型 条件

ArgumentException

path 是一个零长度字符串,仅包含空白、包含一个或多个由 InvalidPathChars 定义的无效字符或包含通配符。

- 或 -

系统未能检索绝对路径。

SecurityException

调用方没有所需的权限。

ArgumentNullException

path 为 空引用(在 Visual Basic 中为 Nothing)。

NotSupportedException

path 包含冒号(“:”)。

PathTooLongException

指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。

备注

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

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

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

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

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

如果您以短文件名传递,则它不被扩展成长文件名。

如果路径没有包含有效字符,则该路径无效,除非它包含后跟任意数目空格的一个或多个“.”字符,然后将它作为“.”或“..”进行分析。

有关使用此方法的示例,请参见下面的“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

检索文件扩展名。

GetExtension

只检索路径中的文件名。

GetFileNameWithoutExtension

只检索路径中的目录名。

GetDirectoryName

更改文件扩展名。

ChangeExtension

按大小对目录中的文件排序。

GetFileSystemInfos

确定目录是否存在。

Exists

确定文件是否存在。

Exists

示例

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

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'
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'
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 );
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);
var fileName : String = "myfile.ext";
var path : String = "\\mydir\\";
var fullPath : String;

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

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

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Path 类
Path 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本