DirectoryInfo.Root DirectoryInfo.Root DirectoryInfo.Root DirectoryInfo.Root Property

定義

取得目錄的根目錄部分。Gets the root portion of the directory.

public:
 property System::IO::DirectoryInfo ^ Root { System::IO::DirectoryInfo ^ get(); };
public System.IO.DirectoryInfo Root { get; }
member this.Root : System.IO.DirectoryInfo
Public ReadOnly Property Root As DirectoryInfo

屬性值

表示根目錄的物件。An object that represents the root of the directory.

例外狀況

呼叫端沒有必要的使用權限。The caller does not have the required permission.

範例

下列範例會顯示指定目錄的根位置。The following example displays root locations for specified directories.

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di1 = new DirectoryInfo(@"\\tempshare\tempdir");
            DirectoryInfo di2 = new DirectoryInfo("tempdir");
            DirectoryInfo di3 = new DirectoryInfo(@"x:\tempdir");
            DirectoryInfo di4 = new DirectoryInfo(@"c:\");
            
            Console.WriteLine("The root path of '{0}' is '{1}'", di1.FullName, di1.Root);
            Console.WriteLine("The root path of '{0}' is '{1}'", di2.FullName, di2.Root);
            Console.WriteLine("The root path of '{0}' is '{1}'", di3.FullName, di3.Root);
            Console.WriteLine("The root path of '{0}' is '{1}'", di4.FullName, di4.Root);
        }
    }
}
/* 
This code produces output similar to the following:

The root path of '\\tempshare\tempdir' is '\\tempshare\tempdir'
The root path of 'c:\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\tempdir' is 'c:\'
The root path of 'x:\tempdir' is 'x:\'
The root path of 'c:\' is 'c:\'
Press any key to continue . . .

*/

Imports System.IO

Module Module1

    Sub Main()
        Dim di1 As DirectoryInfo = New DirectoryInfo("\\tempshare\tempdir")
        Dim di2 As DirectoryInfo = New DirectoryInfo("tempdir")
        Dim di3 As DirectoryInfo = New DirectoryInfo("x:\tempdir")
        Dim di4 As DirectoryInfo = New DirectoryInfo("c:\")

        Console.WriteLine("The root path of '{0}' is '{1}'", di1.FullName, di1.Root)
        Console.WriteLine("The root path of '{0}' is '{1}'", di2.FullName, di2.Root)
        Console.WriteLine("The root path of '{0}' is '{1}'", di3.FullName, di3.Root)
        Console.WriteLine("The root path of '{0}' is '{1}'", di4.FullName, di4.Root)
    End Sub

End Module

' This code produces output similar to the following:

' The root path of '\\tempshare\tempdir' is '\\tempshare\tempdir'
' The root path of 'c:\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\tempdir' is 'c:\'
' The root path of 'x:\tempdir' is 'x:\'
' The root path of 'c:\' is 'c:\'
' Press any key to continue . . .

安全性

FileIOPermission
用於讀取檔案和目錄,以及存取路徑。for reading from files and directories and for access to the path. 相關聯的FileIOPermissionAccess列舉:、FileIOPermissionAccessAssociated enumerations: FileIOPermissionAccess, FileIOPermissionAccess

適用於

另請參閱