Path.IsPathRooted Method
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| path contains one or more of the invalid characters defined in InvalidPathChars, or contains a wildcard character. |
This method does not verify that the path or file name exists.
IsPathRooted returns true for path strings such as "\\MyDir\\MyFile.txt" and "C:\\MyDir". It returns false for path strings such as "MyDir".
For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | |
| Write to a text file. | |
| Read from a text file. | |
| Retrieve the fully qualified path of a file. | |
| Retrieve only the directory name from a path. | |
| Determine if a directory exists. |
The following code example demonstrates a use of the IsPathRooted property.
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\\";
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Reference
Path ClassPath Members
System.IO Namespace
Other Resources
File and Stream I/OHow to: Read Text from a File
How to: Write Text to a File