語言

Directory.GetFileSystemEntries 方法

定義

回傳所有符合指定條件的檔案與子目錄名稱。

多載

名稱 Description
GetFileSystemEntries(String)

回傳指定路徑中所有檔案與子目錄的名稱。

GetFileSystemEntries(String, String)

回傳一組與特定路徑中搜尋模式相符的檔案名稱與目錄名稱陣列。

GetFileSystemEntries(String, String, EnumerationOptions)

回傳一組與特定路徑中搜尋模式及列舉選項相符的檔案名稱與目錄名稱陣列。

GetFileSystemEntries(String, String, SearchOption)

回傳指定路徑中與搜尋模式相符的所有檔案名稱與目錄名稱陣列,並可選擇性地搜尋子目錄。

GetFileSystemEntries(String)

來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs

回傳指定路徑中所有檔案與子目錄的名稱。

public:
 static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path);
public static string[] GetFileSystemEntries(string path);
static member GetFileSystemEntries : string -> string[]
Public Shared Function GetFileSystemEntries (path As String) As String()

參數

path
String

指向目錄的相對或絕對路徑。 此字串不區分大小寫。

傳回

String[]

指定目錄中檔案與子目錄名稱的陣列,若找不到檔案或子目錄則為空陣列。

例外狀況

來電者沒有所需的權限。

.NET Framework 與 .NET Core 版本 2.1 以前: path 為零長度字串、僅含空白,或包含一個或多個無效字元。 你可以用 GetInvalidPathChars()來查詢無效字元。

path 是 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

path 是一個檔案名稱。

指定的路徑無效(例如,它位於未對應的磁碟驅動器上)。

範例

以下範例使用此 GetFileSystemEntries 方法,將使用者指定位置中所有檔案與子目錄的名稱填入一組字串,並將陣列中的每個字串列印到主控台。 範例設定為捕捉此方法常見的所有錯誤。

using System;

namespace GetFileSystemEntries
{
    class Class1
    {
        static void Main(string[] args)
        {
            Class1 snippets = new Class1();

            string path = System.IO.Directory.GetCurrentDirectory();
            string filter = "*.exe";

            snippets.PrintFileSystemEntries(path);
            snippets.PrintFileSystemEntries(path, filter);		
            snippets.GetLogicalDrives();
            snippets.GetParent(path);
            snippets.Move("C:\\proof", "C:\\Temp");
        }

        void PrintFileSystemEntries(string path)
        {
            
            try
            {
                // Obtain the file system entries in the directory path.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }
        void PrintFileSystemEntries(string path, string pattern)
        {
            try
            {
                // Obtain the file system entries in the directory
                // path that match the pattern.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path, pattern);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }

        // Print out all logical drives on the system.
        void GetLogicalDrives()
        {
            try
            {
                string[] drives = System.IO.Directory.GetLogicalDrives();

                foreach (string str in drives)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("An I/O error occurs.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
        }
        void GetParent(string path)
        {
            try
            {
                System.IO.DirectoryInfo directoryInfo =
                    System.IO.Directory.GetParent(path);

                System.Console.WriteLine(directoryInfo.FullName);
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, or " +
                    "contains invalid characters.");
            }
        }
        void Move(string sourcePath, string destinationPath)
        {
            try
            {
                System.IO.Directory.Move(sourcePath, destinationPath);
                System.Console.WriteLine("The directory move is complete.");
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");	
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("An attempt was made to move a " +
                    "directory to a different " +
                    "volume, or destDirName " +
                    "already exists.");
            }
        }
    }
}
open System
open System.IO
open System.Security

let printFileSystemEntries path =
    try
        // Obtain the file system entries in the directory path.
        let directoryEntries = Directory.GetFileSystemEntries path

        for str in directoryEntries do
            printfn $"{str}"
    with
    | :? ArgumentNullException ->
        printfn "Path is a null reference."
    | :? SecurityException ->
        printfn $"The caller does not have the required permission."
    | :? ArgumentException ->
        printfn $"Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? DirectoryNotFoundException ->
        printfn $"The path encapsulated in the Directory object does not exist."

let printFileSystemEntriesPattern path pattern =
    try
        // Obtain the file system entries in the directory
        // path that match the pattern.
        let directoryEntries = Directory.GetFileSystemEntries(path, pattern)

        for str in directoryEntries do
            printfn $"{str}"
    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? SecurityException -> printfn "The caller does not have the required permission."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? DirectoryNotFoundException -> printfn "The path encapsulated in the Directory object does not exist."

// Print out all logical drives on the system.
let getLogicalDrives () =
    try
        let drives = Directory.GetLogicalDrives()

        for str in drives do
            printfn $"{str}"
    with
    | :? IOException -> printfn "An I/O error occurs."
    | :? SecurityException -> printfn "The caller does not have the required permission."

let getParent path =
    try
        let directoryInfo = Directory.GetParent path
        printfn $"{directoryInfo.FullName}"

    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."

let move sourcePath destinationPath =
    try
        Directory.Move(sourcePath, destinationPath)
        printfn "The directory move is complete."

    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? SecurityException -> printfn "The caller does not have the required permission."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? IOException -> printfn "An attempt was made to move a directory to a different volume, or destDirName already exists."

let path = Directory.GetCurrentDirectory()
let filter = "*.exe"

printFileSystemEntries path
printFileSystemEntriesPattern path filter
getLogicalDrives ()
getParent path
move "C:\\proof" "C:\\Temp"
Option Explicit On 
Option Strict On

Namespace GetFileSystemEntries
    Class Class1
        Overloads Shared Sub Main(ByVal args() As String)
            Dim snippets As New Class1()
            Dim path As String = System.IO.Directory.GetCurrentDirectory()
            Dim filter As String = "*.exe"
            snippets.PrintFileSystemEntries(path)
            snippets.PrintFileSystemEntries(path, filter)
            snippets.GetLogicalDrives()
            snippets.GetParent(path)
            snippets.Move("C:\proof", "C:\Temp")
        End Sub

        Sub PrintFileSystemEntries(ByVal path As String)
            Try
                ' Obtain the file system entries in the directory path.
                Dim directoryEntries As String()
                directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub
        Sub PrintFileSystemEntries(ByVal path As String, _
                                   ByVal pattern As String)
            Try
                ' Obtain the file system entries in the directory
                ' path that match the pattern.
                Dim directoryEntries As String()
                directoryEntries = _
                   System.IO.Directory.GetFileSystemEntries(path, pattern)

                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub

        ' Print out all logical drives on the system.
        Sub GetLogicalDrives()
            Try
                Dim drives As String()
                drives = System.IO.Directory.GetLogicalDrives()

                Dim str As String
                For Each str In drives
                    System.Console.WriteLine(str)
                Next str
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An I/O error occurs.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            End Try
        End Sub
        Sub GetParent(ByVal path As String)
            Try
                Dim directoryInfo As System.IO.DirectoryInfo
                directoryInfo = System.IO.Directory.GetParent(path)
                System.Console.WriteLine(directoryInfo.FullName)
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                     "contains only white spaces, or " + _
                                     "contains invalid characters.")
            End Try
        End Sub
        Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
            Try
                System.IO.Directory.Move(sourcePath, destinationPath)
                System.Console.WriteLine("The directory move is complete.")
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An attempt was made to move a " + _
                                        "directory to a different " + _
                                        "volume, or destDirName " + _
                                        "already exists.")
            End Try
        End Sub
    End Class
End Namespace

備註

回傳檔案與目錄名稱的順序不保證;如果需要特定的排序順序,請使用此 Sort 方法。

EnumerateFileSystemEntries和 GetFileSystemEntries 方法的不同如下:當你使用 EnumerateFileSystemEntries時,可以在整個集合回傳前開始枚舉該集合;使用 GetFileSystemEntries時,必須等待整個陣列回傳完才能存取陣列。 因此,當你處理多個檔案和目錄時, EnumerateFileSystemEntries 效率會更高。

此方法與 GetFileSystemEntries 搜尋模式中指定的星號(*)相同。

path參數允許指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory。

參數的 path 大小寫區分對應於執行程式碼的檔案系統。 例如,在 NTFS(預設 Windows 檔案系統)上它是不區分大小寫的,在 Linux 檔案系統上則是不區分大小寫。

關於常見 I/O 任務的清單,請參見 Common I/O 任務。

另請參閱

適用於

GetFileSystemEntries(String, String)

來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs

回傳一組與特定路徑中搜尋模式相符的檔案名稱與目錄名稱陣列。

public:
 static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern);
public static string[] GetFileSystemEntries(string path, string searchPattern);
static member GetFileSystemEntries : string * string -> string[]
Public Shared Function GetFileSystemEntries (path As String, searchPattern As String) As String()

參數

path
String

指向目錄的相對或絕對路徑。 此字串不區分大小寫。

searchPattern
String

搜尋字串用來與檔案和目錄名稱相匹配。path 此參數可包含有效的文字路徑與通配符(* 與 ?)字元,但不支援正則表達式。

傳回

String[]

一組符合指定搜尋條件的檔案名稱與目錄名稱陣列,若找不到檔案或目錄,則為空陣列。

例外狀況

來電者沒有所需的權限。

.NET Framework 與 .NET Core 版本 2.1 以前: path 為零長度字串、僅含空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

-或-

searchPattern 不包含有效的模式。

path 或 searchPattern 為 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

path 是一個檔案名稱。

指定的路徑無效(例如,它位於未對應的磁碟驅動器上)。

範例

以下範例使用此 GetFileSystemEntries 方法,將所有符合使用者指定濾波器的檔案名稱填入一組字串,並將陣列中的每個字串列印到主控台。 範例設定為捕捉此方法常見的所有錯誤。

using System;

namespace GetFileSystemEntries
{
    class Class1
    {
        static void Main(string[] args)
        {
            Class1 snippets = new Class1();

            string path = System.IO.Directory.GetCurrentDirectory();
            string filter = "*.exe";

            snippets.PrintFileSystemEntries(path);
            snippets.PrintFileSystemEntries(path, filter);		
            snippets.GetLogicalDrives();
            snippets.GetParent(path);
            snippets.Move("C:\\proof", "C:\\Temp");
        }

        void PrintFileSystemEntries(string path)
        {
            
            try
            {
                // Obtain the file system entries in the directory path.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }
        void PrintFileSystemEntries(string path, string pattern)
        {
            try
            {
                // Obtain the file system entries in the directory
                // path that match the pattern.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path, pattern);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }

        // Print out all logical drives on the system.
        void GetLogicalDrives()
        {
            try
            {
                string[] drives = System.IO.Directory.GetLogicalDrives();

                foreach (string str in drives)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("An I/O error occurs.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
        }
        void GetParent(string path)
        {
            try
            {
                System.IO.DirectoryInfo directoryInfo =
                    System.IO.Directory.GetParent(path);

                System.Console.WriteLine(directoryInfo.FullName);
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, or " +
                    "contains invalid characters.");
            }
        }
        void Move(string sourcePath, string destinationPath)
        {
            try
            {
                System.IO.Directory.Move(sourcePath, destinationPath);
                System.Console.WriteLine("The directory move is complete.");
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");	
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("An attempt was made to move a " +
                    "directory to a different " +
                    "volume, or destDirName " +
                    "already exists.");
            }
        }
    }
}
open System
open System.IO
open System.Security

let printFileSystemEntries path =
    try
        // Obtain the file system entries in the directory path.
        let directoryEntries = Directory.GetFileSystemEntries path

        for str in directoryEntries do
            printfn $"{str}"
    with
    | :? ArgumentNullException ->
        printfn "Path is a null reference."
    | :? SecurityException ->
        printfn $"The caller does not have the required permission."
    | :? ArgumentException ->
        printfn $"Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? DirectoryNotFoundException ->
        printfn $"The path encapsulated in the Directory object does not exist."

let printFileSystemEntriesPattern path pattern =
    try
        // Obtain the file system entries in the directory
        // path that match the pattern.
        let directoryEntries = Directory.GetFileSystemEntries(path, pattern)

        for str in directoryEntries do
            printfn $"{str}"
    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? SecurityException -> printfn "The caller does not have the required permission."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? DirectoryNotFoundException -> printfn "The path encapsulated in the Directory object does not exist."

// Print out all logical drives on the system.
let getLogicalDrives () =
    try
        let drives = Directory.GetLogicalDrives()

        for str in drives do
            printfn $"{str}"
    with
    | :? IOException -> printfn "An I/O error occurs."
    | :? SecurityException -> printfn "The caller does not have the required permission."

let getParent path =
    try
        let directoryInfo = Directory.GetParent path
        printfn $"{directoryInfo.FullName}"

    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."

let move sourcePath destinationPath =
    try
        Directory.Move(sourcePath, destinationPath)
        printfn "The directory move is complete."

    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? SecurityException -> printfn "The caller does not have the required permission."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? IOException -> printfn "An attempt was made to move a directory to a different volume, or destDirName already exists."

let path = Directory.GetCurrentDirectory()
let filter = "*.exe"

printFileSystemEntries path
printFileSystemEntriesPattern path filter
getLogicalDrives ()
getParent path
move "C:\\proof" "C:\\Temp"
Option Explicit On 
Option Strict On

Namespace GetFileSystemEntries
    Class Class1
        Overloads Shared Sub Main(ByVal args() As String)
            Dim snippets As New Class1()
            Dim path As String = System.IO.Directory.GetCurrentDirectory()
            Dim filter As String = "*.exe"
            snippets.PrintFileSystemEntries(path)
            snippets.PrintFileSystemEntries(path, filter)
            snippets.GetLogicalDrives()
            snippets.GetParent(path)
            snippets.Move("C:\proof", "C:\Temp")
        End Sub

        Sub PrintFileSystemEntries(ByVal path As String)
            Try
                ' Obtain the file system entries in the directory path.
                Dim directoryEntries As String()
                directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub
        Sub PrintFileSystemEntries(ByVal path As String, _
                                   ByVal pattern As String)
            Try
                ' Obtain the file system entries in the directory
                ' path that match the pattern.
                Dim directoryEntries As String()
                directoryEntries = _
                   System.IO.Directory.GetFileSystemEntries(path, pattern)

                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub

        ' Print out all logical drives on the system.
        Sub GetLogicalDrives()
            Try
                Dim drives As String()
                drives = System.IO.Directory.GetLogicalDrives()

                Dim str As String
                For Each str In drives
                    System.Console.WriteLine(str)
                Next str
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An I/O error occurs.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            End Try
        End Sub
        Sub GetParent(ByVal path As String)
            Try
                Dim directoryInfo As System.IO.DirectoryInfo
                directoryInfo = System.IO.Directory.GetParent(path)
                System.Console.WriteLine(directoryInfo.FullName)
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                     "contains only white spaces, or " + _
                                     "contains invalid characters.")
            End Try
        End Sub
        Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
            Try
                System.IO.Directory.Move(sourcePath, destinationPath)
                System.Console.WriteLine("The directory move is complete.")
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An attempt was made to move a " + _
                                        "directory to a different " + _
                                        "volume, or destDirName " + _
                                        "already exists.")
            End Try
        End Sub
    End Class
End Namespace

備註

回傳檔案與目錄名稱的順序不保證;如果需要特定的排序順序,請使用此 Sort 方法。

searchPattern 它可以是字面字元和萬用字元的組合,但它不支援正則表達式。 以下允許的萬用字元規定符。searchPattern

萬用字元指定器 比對
* (星號) 沒有或多個角色處於那個位置。
? (問號) 只有一個角色處於那個位置。

除了萬用卡以外的角色,都是字面上的字面角色。 例如, searchPattern 字串「*t」搜尋所有以 path 字母「t」結尾的名字。 searchPattern字串「s*」搜尋所有以字母「s」開頭的path名稱。

searchPattern 不能以兩個句號(“..”)結尾,也不能包含兩個句號(“..”)後接 DirectorySeparatorChar 或 AltDirectorySeparatorChar,也不能包含任何無效字元。 你可以用這個 GetInvalidPathChars 方法查詢無效字元。

Note

當你在像「*.txt」這樣的字元中使用星號萬用字 searchPattern 元時,指定副檔名中的字元數會影響搜尋結果,如下:

  • 若指定的副檔名正好為三個字元長,該方法會回傳以指定副檔名開頭的檔案。 例如,「*.xls」會回傳「book.xls」和「book.xlsx」。
  • 在其他所有情況下,該方法回傳的檔案都完全符合指定的副檔名。 例如,「*.ai」回傳「file.ai」,但不會回傳「file.aif」。

當你使用問號萬用字元時,這個方法只會回傳與指定副檔名相符的檔案。 例如,給定兩個檔案「file1.txt」和「file1.txtother」,在一個目錄中,搜尋模式為「file?」.txt“只會回傳第一個檔案,而搜尋「file*.txt」則會回傳兩個檔案。

path參數允許指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory。

參數的 path 大小寫區分對應於執行程式碼的檔案系統。 例如,在 NTFS(預設 Windows 檔案系統)上它是不區分大小寫的,在 Linux 檔案系統上則是不區分大小寫。

關於常見 I/O 任務的清單,請參見 Common I/O 任務。

另請參閱

適用於

GetFileSystemEntries(String, String, EnumerationOptions)

來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs

回傳一組與特定路徑中搜尋模式及列舉選項相符的檔案名稱與目錄名稱陣列。

public:
 static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public static string[] GetFileSystemEntries(string path, string searchPattern, System.IO.EnumerationOptions enumerationOptions);
static member GetFileSystemEntries : string * string * System.IO.EnumerationOptions -> string[]
Public Shared Function GetFileSystemEntries (path As String, searchPattern As String, enumerationOptions As EnumerationOptions) As String()

參數

path
String

指向目錄的相對或絕對路徑。 此字串不區分大小寫。

searchPattern
String

搜尋字串用來與檔案和目錄名稱相匹配。path 此參數可包含有效文字與通配字元的組合,但不支援正規表達式。

enumerationOptions
EnumerationOptions

一個描述搜尋與列舉配置的物件。

傳回

String[]

一組符合指定搜尋模式與列舉選項的檔案名稱與目錄名稱陣列,若找不到檔案或目錄則為空陣列。

例外狀況

來電者沒有所需的權限。

.NET Framework 與 .NET Core 版本 2.1 以前: path 為零長度字串、僅含空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

-或-

searchPattern 不包含有效的模式。

path 或 searchPattern 為 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

path 是一個檔案名稱。

指定的路徑無效(例如,它位於未對應的磁碟驅動器上)。

備註

回傳檔案與目錄名稱的順序不保證;如果需要特定的排序順序,請使用此 Sort 方法。

searchPattern 它可以是字面字元和萬用字元的組合,但它不支援正則表達式。 以下允許的萬用字元規定符。searchPattern

萬用字元指定器 比對
* (星號) 沒有或多個角色處於那個位置。
? (問號) 只有一個角色處於那個位置。

除了萬用卡以外的角色,都是字面上的字面角色。 例如, searchPattern 字串「*t」搜尋所有以 path 字母「t」結尾的名字。 searchPattern字串「s*」搜尋所有以字母「s」開頭的path名稱。

searchPattern 不能以兩個句號(“..”)結尾,也不能包含兩個句號(“..”)後接 DirectorySeparatorChar 或 AltDirectorySeparatorChar,也不能包含任何無效字元。 你可以用這個 GetInvalidPathChars 方法查詢無效字元。

Note

當你在像「*.txt」這樣的字元中使用星號萬用字 searchPattern 元時,指定副檔名中的字元數會影響搜尋結果,如下:

  • 若指定的副檔名正好為三個字元長,該方法會回傳以指定副檔名開頭的檔案。 例如,「*.xls」會回傳「book.xls」和「book.xlsx」。
  • 在其他所有情況下,該方法回傳的檔案都完全符合指定的副檔名。 例如,「*.ai」回傳「file.ai」,但不會回傳「file.aif」。

當你使用問號萬用字元時,這個方法只會回傳與指定副檔名相符的檔案。 例如,給定兩個檔案「file1.txt」和「file1.txtother」,在一個目錄中,搜尋模式為「file?」.txt“只會回傳第一個檔案,而搜尋「file*.txt」則會回傳兩個檔案。

path參數允許指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory。

參數的 path 大小寫區分對應於執行程式碼的檔案系統。 例如,在 NTFS(預設 Windows 檔案系統)上它是不區分大小寫的,在 Linux 檔案系統上則是不區分大小寫。

關於常見 I/O 任務的清單,請參見 Common I/O 任務。

適用於

GetFileSystemEntries(String, String, SearchOption)

來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs
來源:
Directory.cs

回傳指定路徑中與搜尋模式相符的所有檔案名稱與目錄名稱陣列,並可選擇性地搜尋子目錄。

public:
 static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern, System::IO::SearchOption searchOption);
public static string[] GetFileSystemEntries(string path, string searchPattern, System.IO.SearchOption searchOption);
static member GetFileSystemEntries : string * string * System.IO.SearchOption -> string[]
Public Shared Function GetFileSystemEntries (path As String, searchPattern As String, searchOption As SearchOption) As String()

參數

path
String

指向目錄的相對或絕對路徑。 此字串不區分大小寫。

searchPattern
String

搜尋字串用來與檔案和目錄名稱相匹配。path 此參數可包含有效的文字路徑與通配符(* 與 ?)字元,但不支援正則表達式。

searchOption
SearchOption

列舉值之一,指定搜尋操作應只包含目前目錄,或應包含所有子目錄。 預設值為 TopDirectoryOnly。

傳回

String[]

一個檔案陣列、檔案名稱和目錄名稱,符合指定搜尋條件;若找不到檔案或目錄,則為空陣列。

例外狀況

.NET Framework 及 2.1 .NET Core 版本:path 為零長度字串,僅含空白,或包含無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

-或-

searchPattern 不包含有效的模式。

path 是 null。

-或-

searchPattern 是 null。

searchOption 不是一個有效的 SearchOption 值。

path 不成立,例如指未映射的硬碟。

path 是一個檔案名稱。

指定的路徑、檔案名稱或組合長度超過系統定義的最大長度。

來電者沒有所需的權限。

來電者沒有所需的權限。

備註

回傳檔案與目錄名稱的順序不保證;如果需要特定的排序順序,請使用此 Sort 方法。

searchPattern 它可以是字面字元和萬用字元的組合,但它不支援正則表達式。 以下允許的萬用字元規定符。searchPattern

萬用字元指定器 比對
* (星號) 沒有或多個角色處於那個位置。
? (問號) 只有一個角色處於那個位置。

除了萬用卡以外的角色,都是字面上的字面角色。 例如, searchPattern 字串「*t」搜尋所有以 path 字母「t」結尾的名字。 searchPattern字串「s*」搜尋所有以字母「s」開頭的path名稱。

searchPattern 不能以兩個句號(“..”)結尾,也不能包含兩個句號(“..”)後接 DirectorySeparatorChar 或 AltDirectorySeparatorChar,也不能包含任何無效字元。 你可以用這個 GetInvalidPathChars 方法查詢無效字元。

Note

當你在像「*.txt」這樣的字元中使用星號萬用字 searchPattern 元時,指定副檔名中的字元數會影響搜尋結果,如下:

  • 若指定的副檔名正好為三個字元長,該方法會回傳以指定副檔名開頭的檔案。 例如,「*.xls」會回傳「book.xls」和「book.xlsx」。
  • 在其他所有情況下,該方法回傳的檔案都完全符合指定的副檔名。 例如,「*.ai」回傳「file.ai」,但不會回傳「file.aif」。

當你使用問號萬用字元時,這個方法只會回傳與指定副檔名相符的檔案。 例如,給定兩個檔案「file1.txt」和「file1.txtother」,在一個目錄中,搜尋模式為「file?」.txt“只會回傳第一個檔案,而搜尋「file*.txt」則會回傳兩個檔案。

EnumerateFileSystemEntries和 GetFileSystemEntries 方法的不同如下:當你使用 EnumerateFileSystemEntries時,可以在整個集合回傳前開始枚舉該集合;使用 GetFileSystemEntries時,必須等待整個陣列回傳完才能存取陣列。 因此,當你處理多個檔案和目錄時, EnumerateFileSystemEntries 效率會更高。

你可以用參數 path 來指定相對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的資訊,你可以透過此 GetCurrentDirectory 方法來判斷。

另請參閱

適用於