Path.GetPathRoot Method
Assembly: mscorlib (in mscorlib.dll)
Parameters
- path
The path from which to obtain root directory information.
Return Value
A string containing the root directory of path, such as "C:\", or a null reference (Nothing in Visual Basic) if path is a null reference (Nothing in Visual Basic), or an empty string if path does not contain root directory information.| Exception type | Condition |
|---|---|
| path contains one or more of the invalid characters defined in InvalidPathChars, or contains a wildcard character. -or- String.Empty was passed to path. |
The .NET Framework does not support direct access to physical disks through paths that are device names, such as "\\.\PHYSICALDRIVE0 ".
This method does not verify that the path or file name exists.
Possible patterns for the string returned by this method are as follows:
-
An empty string (path specified a relative path on the current drive or volume).
-
"/" (path specified an absolute path on the current drive).
-
"X:" (path specified a relative path on a drive, where X represents a drive or volume letter).
-
"X:/" (path specified an absolute path on a given drive).
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 a file extension. | |
| Retrieve the fully qualified path of a file. | |
| Retrieve the file name and extension from a path. | |
| Retrieve only the file name from a path. | |
| Retrieve only the directory name from a path. | |
| Change the extension of a file. | |
| Determine if a file exists. |
The following code example demonstrates a use of the GetPathRoot method.
string path = @"\mydir\"; string fileName = "myfile.ext"; string fullPath = @"C:\mydir\myfile.ext"; string pathRoot; pathRoot = Path.GetPathRoot(path); Console.WriteLine("GetPathRoot('{0}') returns '{1}'", path, pathRoot); pathRoot = Path.GetPathRoot(fileName); Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fileName, pathRoot); pathRoot = Path.GetPathRoot(fullPath); Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fullPath, pathRoot); // This code produces output similar to the following: // // GetPathRoot('\mydir\') returns '\' // GetPathRoot('myfile.ext') returns '' // GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'
String path = "\\mydir\\";
String fileName = "myfile.ext";
String fullPath = "C:\\mydir\\myfile.ext";
String pathRoot;
pathRoot = Path.GetPathRoot(path);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", path, pathRoot);
pathRoot = Path.GetPathRoot(fileName);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
fileName, pathRoot);
pathRoot = Path.GetPathRoot(fullPath);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
fullPath, pathRoot);
var path : String = "\\mydir\\"; var fileName : String = "myfile.ext"; var fullPath : String = "C:\\mydir\\myfile.ext"; var pathRoot : String; pathRoot = Path.GetPathRoot(path); Console.WriteLine("GetPathRoot('{0}') returns '{1}'", path, pathRoot); pathRoot = Path.GetPathRoot(fileName); Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fileName, pathRoot); pathRoot = Path.GetPathRoot(fullPath); Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fullPath, pathRoot);
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