Windows apps
Collapse the table of content
Expand the table of content
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.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static string GetPathRoot (
	string path
)
public static String GetPathRoot (
	String path
)
public static function GetPathRoot (
	path : String
) : String
Not applicable.

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

Exception typeCondition

ArgumentException

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

-or-

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 a list of common I/O tasks, see Common I/O Tasks.

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

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

// This code produces output similar to the following:
//
// GetPathRoot('\mydir\') returns '\'
// GetPathRoot('myfile.ext') returns ''
// GetPathRoot('C:\mydir\myfile.ext') returns '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);

var path : String = "\\mydir\\";
var fileName : String = "myfile.ext";
var fullPath : String = "C:\\mydir\\myfile.ext";
var pathRoot : String;

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2017 Microsoft