DirectoryInfo.GetDirectories 方法

定义

返回当前目录的子目录。Returns the subdirectories of the current directory.

重载

GetDirectories()

返回当前目录的子目录。Returns the subdirectories of the current directory.

GetDirectories(String)

返回当前 DirectoryInfo 中与给定搜索条件匹配的目录的数组。Returns an array of directories in the current DirectoryInfo matching the given search criteria.

GetDirectories(String, EnumerationOptions)

返回当前 DirectoryInfo 中与指定的搜索模式和枚举选项匹配的目录的数组。Returns an array of directories in the current DirectoryInfo matching the specified search pattern and enumeration options.

GetDirectories(String, SearchOption)

返回当前 DirectoryInfo 中目录的数组,该数组与给定的搜索条件匹配并使用某个值确定是否搜索子目录。Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.

GetDirectories()

返回当前目录的子目录。Returns the subdirectories of the current directory.

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

返回

一个 DirectoryInfo 对象数组。An array of DirectoryInfo objects.

异常

封装在 DirectoryInfo 对象中的路径无效,例如,在未映射的驱动器上。The path encapsulated in the DirectoryInfo object is invalid, such as being on an unmapped drive.

调用方没有所要求的权限。The caller does not have the required permission.

调用方没有所要求的权限。The caller does not have the required permission.

示例

下面的示例检索根目录中的所有目录,并显示目录名称。The following example retrieves all the directories in the root directory and displays the directory names.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\" );
   
   // Get a reference to each directory in that directory.
   array<DirectoryInfo^>^diArr = di->GetDirectories();
   
   // Display the names of the directories.
   Collections::IEnumerator^ myEnum = diArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DirectoryInfo^ dri = safe_cast<DirectoryInfo^>(myEnum->Current);
      Console::WriteLine( dri->Name );
   }
}

using System;
using System.IO;

public class GetDirectoriesTest 
{
    public static void Main() 
    {

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

        // Get a reference to each directory in that directory.
        DirectoryInfo[] diArr = di.GetDirectories();

        // Display the names of the directories.
        foreach (DirectoryInfo dri in diArr)
            Console.WriteLine(dri.Name);
    }
}
Imports System.IO

Public Class GetDirectoriesTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("c:\")
        ' Get a reference to each directory in that directory.
        Dim diArr As DirectoryInfo() = di.GetDirectories()
        ' Display the names of the directories.
        Dim dri As DirectoryInfo
        For Each dri In diArr
            Console.WriteLine(dri.Name)
        Next dri
    End Sub
End Class

注解

如果没有子目录,则此方法返回一个空数组。If there are no subdirectories, this method returns an empty array. 此方法不是递归的。This method is not recursive.

此方法预先填充以下 DirectoryInfo 属性的值:This method pre-populates the values of the following DirectoryInfo properties:

另请参阅

GetDirectories(String)

返回当前 DirectoryInfo 中与给定搜索条件匹配的目录的数组。Returns an array of directories in the current DirectoryInfo matching the given search criteria.

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

参数

searchPattern
String

要与目录名匹配的搜索字符串。The search string to match against the names of directories. 此参数可以包含有效文本路径和通配符(* 和 ?)的组合,但不支持正则表达式。This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

返回

DirectoryInfo 匹配的 searchPattern 类型的数组。An array of type DirectoryInfo matching searchPattern.

异常

searchPattern 包含一个或多个 GetInvalidPathChars() 方法定义的无效字符。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

封装在 DirectoryInfo 对象中的路径无效(例如,在未映射的驱动器上)。The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

调用方没有所要求的权限。The caller does not have the required permission.

示例

下面的示例统计包含指定字母的路径中的目录。The following example counts the directories in a path that contain the specified letter.

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\" );
      
      // Get only subdirectories that contain the letter "p."
      array<DirectoryInfo^>^dirs = di->GetDirectories( "*p*" );
      Console::WriteLine( "The number of directories containing the letter p is {0}.", dirs->Length );
      Collections::IEnumerator^ myEnum = dirs->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         DirectoryInfo^ diNext = safe_cast<DirectoryInfo^>(myEnum->Current);
         Console::WriteLine( "The number of files in {0} is {1}", diNext, diNext->GetFiles()->Length );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            DirectoryInfo di = new DirectoryInfo(@"c:\");

            // Get only subdirectories that contain the letter "p."
            DirectoryInfo[] dirs = di.GetDirectories("*p*");
            Console.WriteLine("The number of directories containing the letter p is {0}.", dirs.Length);

            foreach (DirectoryInfo diNext in dirs) 
            {
                Console.WriteLine("The number of files in {0} is {1}", diNext, 
                    diNext.GetFiles().Length);
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo("c:\")

        Try
            'Get only subdirectories that contain the letter "p."
            Dim dirs As DirectoryInfo() = di.GetDirectories("*p*")
            Console.WriteLine("The number of directories containing the letter p is {0}.", dirs.Length)
            Dim diNext As DirectoryInfo
            For Each diNext In dirs
                Console.WriteLine("The number of files in {0} is {1}", diNext, _
                 diNext.GetFiles().Length)
            Next

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

注解

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". "。". searchPattern 字符串 "s*" 搜索 path 以字母 "s" 开头的所有名称。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

此方法预先填充以下 DirectoryInfo 属性的值:This method pre-populates the values of the following DirectoryInfo properties:

另请参阅

GetDirectories(String, EnumerationOptions)

返回当前 DirectoryInfo 中与指定的搜索模式和枚举选项匹配的目录的数组。Returns an array of directories in the current DirectoryInfo matching the specified search pattern and enumeration options.

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

参数

searchPattern
String

要与目录名匹配的搜索字符串。The search string to match against the names of directories. 此参数可以包含有效文本路径和通配符(* 和 ?)的组合,但不支持正则表达式。This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

enumerationOptions
EnumerationOptions

描述要使用的搜索和枚举配置的对象。An object that describes the search and enumeration configuration to use.

返回

DirectoryInfosearchPattern 匹配的 enumerationOptions 类型的数组。An array of type DirectoryInfo matching searchPattern and enumerationOptions.

异常

searchPattern 包含一个或多个 GetInvalidPathChars() 方法定义的无效字符。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

封装在 DirectoryInfo 对象中的路径无效(例如,在未映射的驱动器上)。The path encapsulated in the DirectoryInfo object 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". "。". searchPattern 字符串 "s*" 搜索 path 以字母 "s" 开头的所有名称。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

此方法预先填充以下 DirectoryInfo 属性的值:This method pre-populates the values of the following DirectoryInfo properties:

GetDirectories(String, SearchOption)

返回当前 DirectoryInfo 中目录的数组,该数组与给定的搜索条件匹配并使用某个值确定是否搜索子目录。Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.

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

参数

searchPattern
String

要与目录名匹配的搜索字符串。The search string to match against the names of directories. 此参数可以包含有效文本路径和通配符(* 和 ?)的组合,但不支持正则表达式。This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

searchOption
SearchOption

指定搜索操作是应仅包含当前目录还是应包含所有子目录的枚举值之一。One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories.

返回

DirectoryInfo 匹配的 searchPattern 类型的数组。An array of type DirectoryInfo matching searchPattern.

异常

searchPattern 包含一个或多个 GetInvalidPathChars() 方法定义的无效字符。searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPatternnullsearchPattern is null.

searchOption 不是有效的 SearchOption 值。searchOption is not a valid SearchOption value.

封装在 DirectoryInfo 对象中的路径无效(例如,在未映射的驱动器上)。The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

调用方没有所要求的权限。The caller does not have the required permission.

示例

下面的示例列出了 "c:\" 中以字母 "c" 开头的所有目录和文件。The following example lists all of the directories and files that begin with the letter "c" in "c:\".

using namespace System;
using namespace System::IO;

ref class App
{
public:
    static void Main()
    {
        // Specify the directory you want to manipulate.
        String^ path = "c:\\";
        String^ searchPattern = "c*";

        DirectoryInfo^ di = gcnew DirectoryInfo(path);
        array<DirectoryInfo^>^ directories =
            di->GetDirectories(searchPattern, SearchOption::TopDirectoryOnly);

        array<FileInfo^>^ files =
            di->GetFiles(searchPattern, SearchOption::TopDirectoryOnly);

        Console::WriteLine(
            "Directories that begin with the letter \"c\" in {0}", path);
        for each (DirectoryInfo^ dir in directories)
        {
            Console::WriteLine(
                "{0,-25} {1,25}", dir->FullName, dir->LastWriteTime);
        }

        Console::WriteLine();
        Console::WriteLine(
            "Files that begin with the letter \"c\" in {0}", path);
        for each (FileInfo^ file in files)
        {
            Console::WriteLine(
                "{0,-25} {1,25}", file->Name, file->LastWriteTime);
        }
    } // Main()
}; // App()

int main()
{
    App::Main();
}
using System;
using System.IO;

class App
{
    public static void Main()
    {
        // Specify the directory you want to manipulate.
        string path = @"c:\";
        string searchPattern = "c*";

        DirectoryInfo di = new DirectoryInfo(path);
        DirectoryInfo[] directories = 
            di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);

        FileInfo[] files = 
            di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);

        Console.WriteLine(
            "Directories that begin with the letter \"c\" in {0}", path);
        foreach (DirectoryInfo dir in directories)
        {
            Console.WriteLine(
                "{0,-25} {1,25}", dir.FullName, dir.LastWriteTime);
        }

        Console.WriteLine();
        Console.WriteLine(
            "Files that begin with the letter \"c\" in {0}", path);
        foreach (FileInfo file in files)
        {
            Console.WriteLine(
                "{0,-25} {1,25}", file.Name, file.LastWriteTime);
        }
    } // Main()
} // App()
Imports System.IO

Class App
    Public Shared Sub Main()
        ' Specify the directory you want to manipulate.
        Dim path As String = "c:\\"
        Dim searchPattern As String = "c*"

        Dim di As DirectoryInfo = New DirectoryInfo(path)
        Dim directories() As DirectoryInfo = _
            di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly)

        Dim files() As FileInfo = _
            di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly)

        Console.WriteLine( _
            "Directories that begin with the letter 'c' in {0}", path)
        Dim dir As DirectoryInfo
        For Each dir In directories
            Console.WriteLine( _
                "{0,-25} {1,25}", dir.FullName, dir.LastWriteTime)
        Next dir

        Console.WriteLine()
        Console.WriteLine( _
            "Files that begin with the letter 'c' in {0}", path)
        Dim file As FileInfo
        For Each file In files
            Console.WriteLine( _
                "{0,-25} {1,25}", file.Name, file.LastWriteTime)
        Next file
    End Sub
End Class

注解

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". "。". searchPattern 字符串 "s*" 搜索 path 以字母 "s" 开头的所有名称。The searchPattern string "s*" searches for all names in path beginning with the letter "s".

如果没有子目录,或者没有与 searchPattern 参数匹配的子目录,则此方法将返回一个空数组。If there are no subdirectories, or no subdirectories match the searchPattern parameter, this method returns an empty array.

此方法预先填充以下 DirectoryInfo 属性的值:This method pre-populates the values of the following DirectoryInfo properties:

另请参阅

适用于