Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Path.GetPathRoot Method

Gets the root directory information of the specified path.

Namespace:  System.IO
Assemblies:   mscorlib (in mscorlib.dll)
  System.Runtime.Extensions (in System.Runtime.Extensions.dll)

Public Shared Function GetPathRoot ( _
	path As String _
) As String


Type: System.String

The path from which to obtain root directory information.

Return Value

Type: System.String
The root directory of path, such as "C:\", or Nothing if path is Nothing, or an empty string if path does not contain root directory information.


path contains one or more of the invalid characters defined in GetInvalidPathChars.


String.Empty was passed to path.

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

  • "\\ComputerName\SharedFolder" (a UNC path).

The .NET Framework does not support direct access to physical disks through paths that are device names, such as "\\.\PHYSICALDRIVE0 ".

For a list of common I/O tasks, see Common I/O Tasks.

The following code example demonstrates a use of the GetPathRoot method.

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)

' This code produces output similar to the following: 

' GetPathRoot('\mydir\') returns '\' 
' GetPathRoot('myfile.ext') returns '' 
' GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
© 2015 Microsoft