Path.IsPathRooted 方法

获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。

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

语法

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

returnValue = Path.IsPathRooted(path)
public static bool IsPathRooted (
    string path
)
public:
static bool IsPathRooted (
    String^ path
)
public static boolean IsPathRooted (
    String path
)
public static function IsPathRooted (
    path : String
) : boolean

参数

  • path
    要测试的路径。

返回值

如果 path 包含绝对路径,则为 true;否则为 false

异常

异常类型 条件

ArgumentException

path 包含一个或多个在 InvalidPathChars 中定义的无效字符或包含通配符。

备注

此方法不验证路径或文件名是否存在。

IsPathRooted 为 path 字符串(例如“\\MyDir\\MyFile.txt”和“C:\\MyDir”)返回 true。它为 path 字符串(例如“MyDir”)返回 false

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

若要执行此操作...

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

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

检索文件的完全限定路径。

GetFullPath

只检索路径中的目录名。

GetDirectoryName

确定目录是否存在。

Exists

示例

下面的代码示例演示 IsPathRooted 属性的用法。

Dim fileName As String = "C:\mydir\myfile.ext"
Dim UncPath As String = "\\myPc\mydir\myfile"
Dim relativePath As String = "mydir\sudir\"
Dim result As Boolean

result = Path.IsPathRooted(fileName)
Console.WriteLine("IsPathRooted('{0}') returns {1}", fileName, result)

result = Path.IsPathRooted(UncPath)
Console.WriteLine("IsPathRooted('{0}') returns {1}", UncPath, result)

result = Path.IsPathRooted(relativePath)
Console.WriteLine("IsPathRooted('{0}') returns {1}", relativePath, result)

' This code produces output similar to the following:
'
' IsPathRooted('C:\mydir\myfile.ext') returns True
' IsPathRooted('\\myPc\mydir\myfile') returns True
' IsPathRooted('mydir\sudir\') returns False
string fileName = @"C:\mydir\myfile.ext";
string UncPath = @"\\myPc\mydir\myfile";
string relativePath = @"mydir\sudir\";
bool result;

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

result = Path.IsPathRooted(UncPath);
Console.WriteLine("IsPathRooted('{0}') returns {1}", 
    UncPath, result);

result = Path.IsPathRooted(relativePath);
Console.WriteLine("IsPathRooted('{0}') returns {1}", 
    relativePath, result);

// This code produces output similar to the following:
//
// IsPathRooted('C:\mydir\myfile.ext') returns True
// IsPathRooted('\\myPc\mydir\myfile') returns True
// IsPathRooted('mydir\sudir\') returns False
String^ fileName = "C:\\mydir\\myfile.ext";
String^ UncPath = "\\\\myPc\\mydir\\myfile";
String^ relativePath = "mydir\\sudir\\";
bool result;
result = Path::IsPathRooted( fileName );
Console::WriteLine( "IsPathRooted('{0}') returns {1}", fileName, result.ToString() );
result = Path::IsPathRooted( UncPath );
Console::WriteLine( "IsPathRooted('{0}') returns {1}", UncPath, result.ToString() );
result = Path::IsPathRooted( relativePath );
Console::WriteLine( "IsPathRooted('{0}') returns {1}", relativePath, result.ToString() );
String fileName = "C:\\mydir\\myfile.ext";
String UncPath = "\\\\myPc\\mydir\\myfile";
String relativePath = "mydir\\sudir\\";
boolean result;

result = Path.IsPathRooted(fileName);
Console.WriteLine("IsPathRooted('{0}') returns {1}", 
    fileName, System.Convert.ToString(result));

result = Path.IsPathRooted(UncPath);
Console.WriteLine("IsPathRooted('{0}') returns {1}", 
    UncPath, System.Convert.ToString (result));

result = Path.IsPathRooted(relativePath);
Console.WriteLine("IsPathRooted('{0}') returns {1}", 
    relativePath, System.Convert.ToString (result));
var fileName : String = "C:\\mydir\\myfile.ext";
var UncPath : String = "\\\\myPc\\mydir\\myfile";
var relativePath : String = "mydir\\sudir\\";
var result : boolean;

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

result = Path.IsPathRooted(UncPath);
Console.WriteLine("IsPathRooted('{0}') returns {1}", 
                  UncPath, result);

result = Path.IsPathRooted(relativePath);
Console.WriteLine("IsPathRooted('{0}') returns {1}", 
                  relativePath, result);

平台

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
如何:从文件读取文本
如何:向文件写入文本