Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Path.GetPathRoot Method

Gets the root directory information of the specified path.

[Visual Basic]
Public Shared Function GetPathRoot( _
   ByVal path As String _
) As String
[C#]
public static string GetPathRoot(
 string path
);
[C++]
public: static String* GetPathRoot(
 String* path
);
[JScript]
public static function GetPathRoot(
   path : String
) : String;

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), or an empty string if path does not contain root directory information.

Exceptions

Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in InvalidPathChars.

-or-

String.Empty was passed to path.

Remarks

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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Retrieve a file extension. GetExtension
Retrieve the fully qualified path of a file. GetFullPath
Retrieve the file name and extension from a path. GetFileName
Retrieve only the file name from a path. GetFileNameWithoutExtension
Retrieve only the directory name from a path. GetDirectoryName
Change the extension of a file. ChangeExtension
Determine if a file exists. Exists

Example

[Visual Basic, C#, C++] The following example demonstrates a use of the GetPathRoot method.

[Visual Basic] 
Dim pathname As String = "\mydir\"
Dim fileName As String = "myfile.ext"
Dim fullPath As String = "C:\mydir\myfile.ext"
Dim pathnameRoot As String

pathnameRoot = Path.GetPathRoot(pathname)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", pathname, pathnameRoot)

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

pathnameRoot = Path.GetPathRoot(fullPath)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fullPath, pathnameRoot)

[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);

[C++] 
String * path = S"\\mydir\\";
String * fileName = S"myfile.ext";
String * fullPath = S"C:\\mydir\\myfile.ext";
String * pathRoot;

pathRoot = Path::GetPathRoot(path);
Console::WriteLine(S"GetPathRoot('{0}') returns '{1}'", 
   path, pathRoot);

pathRoot = Path::GetPathRoot(fileName);
Console::WriteLine(S"GetPathRoot('{0}') returns '{1}'", 
   fileName, pathRoot);

pathRoot = Path::GetPathRoot(fullPath);
Console::WriteLine(S"GetPathRoot('{0}') returns '{1}'", 
   fullPath, pathRoot);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Path Class | Path Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft