Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
IsPathRooted Method

Path.IsPathRooted Method

Gets a value indicating whether the specified path string contains a root.

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

'Declaration
Public Shared Function IsPathRooted ( _
	path As String _
) As Boolean

Parameters

path
Type: System.String

The path to test.

Return Value

Type: System.Boolean
true if path contains a root; otherwise, false.

ExceptionCondition
ArgumentException

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

The IsPathRooted method returns true if the first character is a directory separator character such as "\", or if the path starts with a drive letter and colon (:). For example, it returns true for path strings such as "\\MyDir\\MyFile.txt", "C:\\MyDir", or "C:MyDir". It returns false for path strings such as "MyDir".

This method does not verify that the path or file name exists.

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

The following code example demonstrates how the IsPathRooted method can be used to test three strings.

Dim fileName As String = "C:\mydir\myfile.ext" 
Dim UncPath As String = "\\myPc\mydir\myfile" 
Dim relativePath As String = "mydir\sudir\" 
Dim result As 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)

' 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

.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
Show:
© 2015 Microsoft