DirectoryInfo.GetFileSystemInfos DirectoryInfo.GetFileSystemInfos DirectoryInfo.GetFileSystemInfos DirectoryInfo.GetFileSystemInfos Method

定義

擷取強型別 FileSystemInfo 物件的陣列,表示目前目錄的檔案和子目錄。Retrieves an array of strongly typed FileSystemInfo objects representing files and subdirectories of the current directory.

多載

GetFileSystemInfos() GetFileSystemInfos() GetFileSystemInfos() GetFileSystemInfos()

傳回強型別 FileSystemInfo 項目的陣列,表示目錄中的所有檔案和子目錄。Returns an array of strongly typed FileSystemInfo entries representing all the files and subdirectories in a directory.

GetFileSystemInfos(String) GetFileSystemInfos(String) GetFileSystemInfos(String) GetFileSystemInfos(String)

擷取強型別 FileSystemInfo 物件的陣列,表示符合指定之搜尋準則的檔案和子目錄。Retrieves an array of strongly typed FileSystemInfo objects representing the files and subdirectories that match the specified search criteria.

GetFileSystemInfos(String, EnumerationOptions) GetFileSystemInfos(String, EnumerationOptions) GetFileSystemInfos(String, EnumerationOptions)
GetFileSystemInfos(String, SearchOption) GetFileSystemInfos(String, SearchOption) GetFileSystemInfos(String, SearchOption)

擷取 FileSystemInfo 物件的陣列,表示符合指定之搜尋準則的檔案和子目錄。Retrieves an array of FileSystemInfo objects that represent the files and subdirectories matching the specified search criteria.

GetFileSystemInfos() GetFileSystemInfos() GetFileSystemInfos() GetFileSystemInfos()

傳回強型別 FileSystemInfo 項目的陣列,表示目錄中的所有檔案和子目錄。Returns an array of strongly typed FileSystemInfo entries representing all the files and subdirectories in a directory.

public:
 cli::array <System::IO::FileSystemInfo ^> ^ GetFileSystemInfos();
public System.IO.FileSystemInfo[] GetFileSystemInfos ();
member this.GetFileSystemInfos : unit -> System.IO.FileSystemInfo[]
Public Function GetFileSystemInfos () As FileSystemInfo()

傳回

強型別 (Strongly Typed) FileSystemInfo 項目的陣列。An array of strongly typed FileSystemInfo entries.

例外狀況

路徑無效 (例如,位於未對應的磁碟上)。The path is invalid (for example, it is on an unmapped drive).

範例

下列範例會計算指定目錄下的檔案和目錄。The following example counts the files and directories under the specified directory.

using System;
using System.IO;

class DirectoryFileCount
{

    static long files = 0;
    static long directories = 0;


    static void Main()
    {
        try
        {
            Console.WriteLine("Enter the path to a directory:");

            string directory = Console.ReadLine();

            // Create a new DirectoryInfo object.
            DirectoryInfo dir = new DirectoryInfo(directory);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException("The directory does not exist.");
            }

            // Call the GetFileSystemInfos method.
            FileSystemInfo[] infos = dir.GetFileSystemInfos();

            Console.WriteLine("Working...");

            // Pass the result to the ListDirectoriesAndFiles
            // method defined below.
            ListDirectoriesAndFiles(infos);

            // Display the results to the console. 
            Console.WriteLine("Directories: {0}", directories);
            Console.WriteLine("Files: {0}", files);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {

            Console.ReadLine();
        }
    }

    static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo)
    {
        // Check the FSInfo parameter.
        if (FSInfo == null)
        {
            throw new ArgumentNullException("FSInfo");
        }

        // Iterate through each item.
        foreach (FileSystemInfo i in FSInfo)
        {
            // Check to see if this is a DirectoryInfo object.
            if (i is DirectoryInfo)
            {
                // Add one to the directory count.
                directories++;

                // Cast the object to a DirectoryInfo object.
                DirectoryInfo dInfo = (DirectoryInfo)i;

                // Iterate through all sub-directories.
                ListDirectoriesAndFiles(dInfo.GetFileSystemInfos());
            }
            // Check to see if this is a FileInfo object.
            else if (i is FileInfo)
            {
                // Add one to the file count.
                files++;

            }

        }
    }
}
Imports System.IO



Module DirectoryFileCount

    Dim files As Long = 0
    Dim directories As Long = 0



    Sub Main()
        Try
            Console.WriteLine("Enter the path to a directory:")

            Dim directory As String = Console.ReadLine()

            ' Create a new DirectoryInfo object.
            Dim dir As New DirectoryInfo(directory)

            If Not dir.Exists Then
                Throw New DirectoryNotFoundException("The directory does not exist.")
            End If

            ' Call the GetFileSystemInfos method.
            Dim infos As FileSystemInfo() = dir.GetFileSystemInfos()

            Console.WriteLine("Working...")

            ' Pass the result to the ListDirectoriesAndFiles
            ' method defined below.
            ListDirectoriesAndFiles(infos)

            ' Display the results to the console. 
            Console.WriteLine("Directories: {0}", directories)
            Console.WriteLine("Files: {0}", files)

        Catch e As Exception
            Console.WriteLine(e.Message)
        Finally

            Console.ReadLine()
        End Try

    End Sub


    Sub ListDirectoriesAndFiles(ByVal FSInfo() As FileSystemInfo)
        ' Check the FSInfo parameter.
        If FSInfo Is Nothing Then
            Throw New ArgumentNullException("FSInfo")
        End If

        ' Iterate through each item.
        Dim i As FileSystemInfo
        For Each i In FSInfo
            ' Check to see if this is a DirectoryInfo object.
            If TypeOf i Is DirectoryInfo Then
                ' Add one to the directory count.
                directories += 1

                ' Cast the object to a DirectoryInfo object.
                Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)

                ' Iterate through all sub-directories.
                ListDirectoriesAndFiles(dInfo.GetFileSystemInfos())
                ' Check to see if this is a FileInfo object.
            ElseIf TypeOf i Is FileInfo Then
                ' Add one to the file count.
                files += 1
            End If
        Next i

    End Sub
End Module

備註

如果中DirectoryInfo沒有任何檔案或目錄,這個方法會傳回空陣列。If there are no files or directories in the DirectoryInfo, this method returns an empty array. 這個方法不是遞迴的。This method is not recursive.

對於子目錄,這個FileSystemInfo方法所傳回的物件可以轉換成衍生的類別。 DirectoryInfoFor subdirectories, the FileSystemInfo objects returned by this method can be cast to the derived class DirectoryInfo. 使用屬性傳回FileSystemInfo.AttributesFileAttributes值來判斷是否代表FileSystemInfo檔案或目錄。Use the FileAttributes value returned by the FileSystemInfo.Attributes property to determine whether the FileSystemInfo represents a file or a directory.

這個方法會預先填入下列FileSystemInfo屬性的值:This method pre-populates the values of the following FileSystemInfo properties:

另請參閱

GetFileSystemInfos(String) GetFileSystemInfos(String) GetFileSystemInfos(String) GetFileSystemInfos(String)

擷取強型別 FileSystemInfo 物件的陣列,表示符合指定之搜尋準則的檔案和子目錄。Retrieves an array of strongly typed FileSystemInfo objects representing the files and subdirectories that match the specified search criteria.

public:
 cli::array <System::IO::FileSystemInfo ^> ^ GetFileSystemInfos(System::String ^ searchPattern);
public System.IO.FileSystemInfo[] GetFileSystemInfos (string searchPattern);
member this.GetFileSystemInfos : string -> System.IO.FileSystemInfo[]
Public Function GetFileSystemInfos (searchPattern As String) As FileSystemInfo()

參數

searchPattern
String String String String

要比對目錄及檔案名稱的搜尋字串。The search string to match against the names of directories and files. 這個參數可以包含有效常值路徑與萬用 (* 和 ?) 字元的組合,但是不支援規則運算式。This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

傳回

強型別 FileSystemInfo 物件的陣列,符合搜尋準則。An array of strongly typed FileSystemInfo objects matching the search criteria.

例外狀況

searchPattern 包含一或多個 GetInvalidPathChars() 方法所定義的無效字元。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

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

範例

下列範例會計算符合指定搜尋模式的檔案和目錄。The following example counts the files and directories that match the specified search pattern.

using System;
using System.IO;

class DirectoryFileCount
{

    static long files = 0;
    static long directories = 0;


    static void Main()
    {
        try
        {
            Console.WriteLine("Enter the path to a directory:");

            string directory = Console.ReadLine();

            Console.WriteLine("Enter a search string (for example *p*):");

            string searchString = Console.ReadLine();

            // Create a new DirectoryInfo object.
            DirectoryInfo dir = new DirectoryInfo(directory);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException("The directory does not exist.");
            }

            // Call the GetFileSystemInfos method.
            FileSystemInfo[] infos = dir.GetFileSystemInfos(searchString);

            Console.WriteLine("Working...");

            // Pass the result to the ListDirectoriesAndFiles
            // method defined below.
            ListDirectoriesAndFiles(infos, searchString);

            // Display the results to the console. 
            Console.WriteLine("Directories: {0}", directories);
            Console.WriteLine("Files: {0}", files);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {

            Console.ReadLine();
        }
    }

    static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo, string SearchString)
    {
        // Check the parameters.
        if (FSInfo == null)
        {
            throw new ArgumentNullException("FSInfo");
        }
        if (SearchString == null || SearchString.Length == 0)
        {
            throw new ArgumentNullException("SearchString");
        }

        // Iterate through each item.
        foreach (FileSystemInfo i in FSInfo)
        {
            // Check to see if this is a DirectoryInfo object.
            if (i is DirectoryInfo)
            {
                // Add one to the directory count.
                directories++;

                // Cast the object to a DirectoryInfo object.
                DirectoryInfo dInfo = (DirectoryInfo)i;

                // Iterate through all sub-directories.
                ListDirectoriesAndFiles(dInfo.GetFileSystemInfos(SearchString), SearchString);
            }
            // Check to see if this is a FileInfo object.
            else if (i is FileInfo)
            {
                // Add one to the file count.
                files++;

            }

        }
    }
}
Imports System.IO



Module DirectoryFileCount

    Dim files As Long = 0
    Dim directories As Long = 0



    Sub Main()
        Try
            Console.WriteLine("Enter the path to a directory:")

            Dim directory As String = Console.ReadLine()

            Console.WriteLine("Enter a search string (for example *p*):")

            Dim searchString As String = Console.ReadLine()

            ' Create a new DirectoryInfo object.
            Dim dir As New DirectoryInfo(directory)

            If Not dir.Exists Then
                Throw New DirectoryNotFoundException("The directory does not exist.")
            End If

            ' Call the GetFileSystemInfos method.
            Dim infos As FileSystemInfo() = dir.GetFileSystemInfos(searchString)

            Console.WriteLine("Working...")

            ' Pass the result to the ListDirectoriesAndFiles
            ' method defined below.
            ListDirectoriesAndFiles(infos, searchString)

            ' Display the results to the console. 
            Console.WriteLine("Directories: {0}", directories)
            Console.WriteLine("Files: {0}", files)

        Catch e As Exception
            Console.WriteLine(e.Message)
        Finally

            Console.ReadLine()
        End Try

    End Sub


    Sub ListDirectoriesAndFiles(ByVal FSInfo() As FileSystemInfo, ByVal SearchString As String)
        ' Check the parameters.
        If FSInfo Is Nothing Then
            Throw New ArgumentNullException("FSInfo")
        End If
        If SearchString Is Nothing OrElse SearchString.Length = 0 Then
            Throw New ArgumentNullException("SearchString")
        End If

        ' Iterate through each item.
        Dim i As FileSystemInfo
        For Each i In FSInfo
            ' Check to see if this is a DirectoryInfo object.
            If TypeOf i Is DirectoryInfo Then
                ' Add one to the directory count.
                directories += 1

                ' Cast the object to a DirectoryInfo object.
                Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)

                ' Iterate through all sub-directories.
                ListDirectoriesAndFiles(dInfo.GetFileSystemInfos(SearchString), SearchString)
                ' Check to see if this is a FileInfo object.
            ElseIf TypeOf i Is FileInfo Then
                ' Add one to the file count.
                files += 1
            End If
        Next i

    End Sub
End Module

備註

searchPattern可以是常值和萬用字元的組合,但是不支援正則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. searchPattern允許下列萬用字元規範。The following wildcard specifiers are permitted in searchPattern.

萬用字元規範Wildcard specifier 符合Matches
* (星號)* (asterisk) 該位置中的零或多個字元。Zero or more characters in that position.
?? (問號)(question mark) 該位置中的零或一個字元。Zero or one character in that position.

萬用字元以外的字元是常值字元。Characters other than the wildcard are literal characters. 例如,字串 "* t" 會搜尋結尾為字母 "t" 的所有名稱。For example, the string "*t" searches for all names in ending with the letter "t". ".". 字串 "s*" 會path搜尋開頭為字母 "s" 的所有名稱。 searchPatternThe searchPattern string "s*" searches for all names in path beginning with the letter "s".

這個方法不是遞迴的。This method is not recursive.

對於子目錄,這個FileSystemInfo方法所傳回的物件可以轉換成衍生的類別。 DirectoryInfoFor subdirectories, the FileSystemInfo objects returned by this method can be cast to the derived class DirectoryInfo. 使用屬性傳回FileSystemInfo.AttributesFileAttributes值來判斷是否代表FileSystemInfo檔案或目錄。Use the FileAttributes value returned by the FileSystemInfo.Attributes property to determine whether the FileSystemInfo represents a file or a directory.

允許使用萬用字元。Wild cards are permitted. 例如, searchPattern字串 "* t" 會搜尋path結尾為字母 "t" 的所有目錄名稱。For example, the searchPattern string "*t" searches for all directory names in path ending with the letter "t". 字串 "s*" 會搜尋path開頭為字母 "s" 的所有目錄名稱。 searchPatternThe searchPattern string "s*" searches for all directory names in path beginning with the letter "s".

只有在指定為有效目錄名稱的一部分(例如searchPattern目錄名稱 "a...)時,才能在中使用字串": "。b "。The string ".." can only be used in searchPattern if it is specified as a part of a valid directory name, such as in the directory name "a..b". 它不能用來上移目錄階層。It cannot be used to move up the directory hierarchy. 如果沒有檔案或目錄,或沒有任何檔案或目錄符合searchPattern DirectoryInfo中的字串,這個方法會傳回空陣列。If there are no files or directories, or no files or directories that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

這個方法會預先填入下列FileSystemInfo屬性的值:This method pre-populates the values of the following FileSystemInfo properties:

安全性

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

另請參閱

GetFileSystemInfos(String, EnumerationOptions) GetFileSystemInfos(String, EnumerationOptions) GetFileSystemInfos(String, EnumerationOptions)

public:
 cli::array <System::IO::FileSystemInfo ^> ^ GetFileSystemInfos(System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public System.IO.FileSystemInfo[] GetFileSystemInfos (string searchPattern, System.IO.EnumerationOptions enumerationOptions);
member this.GetFileSystemInfos : string * System.IO.EnumerationOptions -> System.IO.FileSystemInfo[]

參數

searchPattern
String String String String

傳回

GetFileSystemInfos(String, SearchOption) GetFileSystemInfos(String, SearchOption) GetFileSystemInfos(String, SearchOption)

擷取 FileSystemInfo 物件的陣列,表示符合指定之搜尋準則的檔案和子目錄。Retrieves an array of FileSystemInfo objects that represent the files and subdirectories matching the specified search criteria.

public:
 cli::array <System::IO::FileSystemInfo ^> ^ GetFileSystemInfos(System::String ^ searchPattern, System::IO::SearchOption searchOption);
public System.IO.FileSystemInfo[] GetFileSystemInfos (string searchPattern, System.IO.SearchOption searchOption);
member this.GetFileSystemInfos : string * System.IO.SearchOption -> System.IO.FileSystemInfo[]

參數

searchPattern
String String String String

要比對目錄及檔案名稱的搜尋字串。The search string to match against the names of directories and files. 這個參數可以包含有效常值路徑與萬用 (* 和 ?) 字元的組合,但是不支援規則運算式。This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

searchOption
SearchOption SearchOption SearchOption SearchOption

其中一個列舉值,指定搜尋作業應該只包含目前目錄還是包含所有子目錄。One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories. 預設值為 TopDirectoryOnlyThe default value is TopDirectoryOnly.

傳回

陣列,包含符合搜尋準則的檔案系統項目。An array of file system entries that match the search criteria.

例外狀況

searchPattern 包含一或多個 GetInvalidPathChars() 方法所定義的無效字元。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

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

備註

searchPattern可以是常值和萬用字元的組合,但是不支援正則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. searchPattern允許下列萬用字元規範。The following wildcard specifiers are permitted in searchPattern.

萬用字元規範Wildcard specifier 符合Matches
* (星號)* (asterisk) 該位置中的零或多個字元。Zero or more characters in that position.
?? (問號)(question mark) 該位置中的零或一個字元。Zero or one character in that position.

萬用字元以外的字元是常值字元。Characters other than the wildcard are literal characters. 例如,字串 "* t" 會搜尋結尾為字母 "t" 的所有名稱。For example, the string "*t" searches for all names in ending with the letter "t". ".". 字串 "s*" 會path搜尋開頭為字母 "s" 的所有名稱。 searchPatternThe searchPattern string "s*" searches for all names in path beginning with the letter "s".

對於子目錄,這個FileSystemInfo方法所傳回的物件可以轉換成衍生的類別。 DirectoryInfoFor subdirectories, the FileSystemInfo objects returned by this method can be cast to the derived class DirectoryInfo. 使用屬性傳回FileSystemInfo.AttributesFileAttributes值來判斷是否代表FileSystemInfo檔案或目錄。Use the FileAttributes value returned by the FileSystemInfo.Attributes property to determine whether the FileSystemInfo represents a file or a directory.

這個方法會預先填入下列FileSystemInfo屬性的值:This method pre-populates the values of the following FileSystemInfo properties:

另請參閱

適用於