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.

DirectoryInfo.Root Property

Gets the root portion of a path.

[Visual Basic]
Public ReadOnly Property Root As DirectoryInfo
[C#]
public DirectoryInfo Root {get;}
[C++]
public: __property DirectoryInfo* get_Root();
[JScript]
public function get Root() : DirectoryInfo;

Property Value

A DirectoryInfo object representing the root of a path.

Exceptions

Exception Type Condition
SecurityException The caller does not have the required permission.

Remarks

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...
Retrieve the fully qualified path of a file. Path.GetFullPath
Retrieve only the directory name from a path. Path.GetDirectoryName
See the files in a directory. Name
See the subdirectories of a directory. Directory.GetDirectories

DirectoryInfo.GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos
Find the size of a directory. Directory
Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Determine if a file exists. Exists
Determine if a directory exists. Exists

Example

The following example demonstrates how to determine the root location of a specified directory.

[Visual Basic] 
Imports System
Imports System.IO

Public Class MoveToTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")

        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
            di.Create()
        End If

        ' Create a subdirectory in the directory just created.
        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
        Console.WriteLine("The root path of '{0}' is '{1}'", dis.Name, dis.Root)

        ' Delete the parent directory.
        di.Delete(True)
    End Sub 'Main
End Class 'MoveToTest

[C#] 
using System;
using System.IO;

public class MoveToTest 
{
    public static void Main() 
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");
        Console.WriteLine("The root path of '{0}' is '{1}'", dis.Name, dis.Root);

        // Delete the parent directory.
        di.Delete(true);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Make a reference to a directory.
    DirectoryInfo* di = new DirectoryInfo(S"TempDir");

    // Create the directory only if it does not already exist.
    if (!di->Exists)
        di->Create();

    // Create a subdirectory in the directory just created.
    DirectoryInfo* dis = di->CreateSubdirectory(S"SubDir");
    Console::WriteLine(S"The root path of '{0}' is '{1}'", dis->Name, dis->Root);

    // Delete the parent directory.
    di->Delete(true);
}

[JScript] 
import System;
import System.IO;

public class MoveToTest {
    public static function Main() {

        // Make a reference to a directory.
        var di : DirectoryInfo = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        var dis : DirectoryInfo = di.CreateSubdirectory("SubDir");

        Console.WriteLine("The root path of '{0}' is '{1}'", dis.Name, dis.Root);

        // Delete the parent directory.
        di.Delete(true);
    }
}
MoveToTest.Main();

Requirements

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

.NET Framework Security: 

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft