This documentation is archived and is not being maintained.

Path.GetPathRoot Method

Gets the root directory information of the specified path.

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


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.


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


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. 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


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

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

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.


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