Path: GetPathRoot

GetPathRoot

Returns the root directory information of the specified path.



 Public Function GetPathRoot(
	  ByRef Path As String ) As String

Parameters

Path
[ByRef] String. The path from which to obtain root directory information.

Return Values

String -  The root directory of Path, such as "C:\", or an empty string if Path does not contain root directory information.

Remarks

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

Possible patterns for the string returned by this method are as follows:

Exceptions

ExceptionCondition
ArgumentException Path contains one or more of the invalid characters defined in GetInvalidPathChars.
-or-
Path is an empty string.

Examples

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

Public Sub Main()
    Const FileName As String = "MyFile.Ext"
    Const PathName As String = "\MyDir\"
    Const FullPath As String = "C:\MyDir\MyFile.Ext"
    
    Dim PathNameRoot As String
    
    PathNameRoot = Path.GetPathRoot(PathName)
    Debug.Print CorString.Format("GetPathRoot('{0}') returns '{1}'", PathName, PathNameRoot)
    
    PathNameRoot = Path.GetPathRoot(FileName)
    Debug.Print CorString.Format("GetPathRoot('{0}') returns '{1}'", FileName, PathNameRoot)
    
    PathNameRoot = Path.GetPathRoot(FullPath)
    Debug.Print CorString.Format("GetPathRoot('{0}') returns '{1}'", FullPath, PathNameRoot)
End Sub

' This example code produces output similar to the following:
'
'    GetPathRoot('\MyDir\') returns '\'
'    GetPathRoot('MyFile.Ext') returns ''
'    GetPathRoot('C:\MyDir\MyFile.Ext') returns 'C:\'

See Also

Project CorLib Overview

Class Path Overview