Directory.GetFiles Directory.GetFiles Directory.GetFiles Directory.GetFiles Method

定義

傳回符合指定準則之檔案的名稱。Returns the names of files that meet specified criteria.

多載

GetFiles(String) GetFiles(String) GetFiles(String) GetFiles(String)

傳回指定目錄中的檔案名稱 (包括路徑)。Returns the names of files (including their paths) in the specified directory.

GetFiles(String, String) GetFiles(String, String) GetFiles(String, String) GetFiles(String, String)

傳回指定目錄中符合指定搜尋模式的檔案名稱 (包括檔案的路徑)。Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

GetFiles(String, String, EnumerationOptions) GetFiles(String, String, EnumerationOptions) GetFiles(String, String, EnumerationOptions)
GetFiles(String, String, SearchOption) GetFiles(String, String, SearchOption) GetFiles(String, String, SearchOption)

傳回指定目錄中符合指定搜尋模式的檔案名稱 (包括檔案的路徑),並使用值判斷是否搜尋子目錄。Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.

GetFiles(String) GetFiles(String) GetFiles(String) GetFiles(String)

傳回指定目錄中的檔案名稱 (包括路徑)。Returns the names of files (including their paths) in the specified directory.

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

參數

path
String String String String

要搜尋之目錄的相對或絕對路徑。The relative or absolute path to the directory to search. 這個字串不會區分大小寫。This string is not case-sensitive.

傳回

String[]

指定之目錄中的檔案完整名稱 (包括路徑) 陣列,如果找不到任何檔案則為空陣列。An array of the full names (including paths) for the files in the specified directory, or an empty array if no files are found.

例外狀況

path 為檔案名稱。path is a file name.

-或--or- 發生網路錯誤。A network error has occurred.

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

path 為零長度字串,只包含空格或包含一或多個無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters. 您可以使用 GetInvalidPathChars() 方法查詢無效字元。You can query for invalid characters by using the GetInvalidPathChars() method.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

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

範例

下列範例示範如何使用GetFiles方法來傳回檔案名稱,從使用者指定的位置。The following example demonstrates how to use the GetFiles method to return file names from a user-specified location. 此範例會設定為擷取所有錯誤通用於這個方法。The example is configured to catch all errors common to this method.

// For Directory::GetFiles and Directory::GetDirectories
// For File::Exists, Directory::Exists
using namespace System;
using namespace System::IO;
using namespace System::Collections;

// Insert logic for processing found files here.
void ProcessFile( String^ path )
{
   Console::WriteLine( "Processed file '{0}'.", path );
}


// Process all files in the directory passed in, recurse on any directories 
// that are found, and process the files they contain.
void ProcessDirectory( String^ targetDirectory )
{
   
   // Process the list of files found in the directory.
   array<String^>^fileEntries = Directory::GetFiles( targetDirectory );
   IEnumerator^ files = fileEntries->GetEnumerator();
   while ( files->MoveNext() )
   {
      String^ fileName = safe_cast<String^>(files->Current);
      ProcessFile( fileName );
   }

   
   // Recurse into subdirectories of this directory.
   array<String^>^subdirectoryEntries = Directory::GetDirectories( targetDirectory );
   IEnumerator^ dirs = subdirectoryEntries->GetEnumerator();
   while ( dirs->MoveNext() )
   {
      String^ subdirectory = safe_cast<String^>(dirs->Current);
      ProcessDirectory( subdirectory );
   }
}

int main( int argc, char *argv[] )
{
   for ( int i = 1; i < argc; i++ )
   {
      String^ path = gcnew String(argv[ i ]);
      if ( File::Exists( path ) )
      {
         
         // This path is a file
         ProcessFile( path );
      }
      else
      if ( Directory::Exists( path ) )
      {
         
         // This path is a directory
         ProcessDirectory( path );
      }
      else
      {
         Console::WriteLine( "{0} is not a valid file or directory.", path );
      }

   }
}

// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory) 
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }
        
    // Insert logic for processing found files here.
    public static void ProcessFile(string path) 
    {
        Console.WriteLine("Processed file '{0}'.", path);	    
    }
}
' For Directory.GetFiles and Directory.GetDirectories
' For File.Exists, Directory.Exists 

Imports System
Imports System.IO
Imports System.Collections

Public Class RecursiveFileProcessor

    Public Overloads Shared Sub Main(ByVal args() As String)
        Dim path As String
        For Each path In args
            If File.Exists(path) Then
                ' This path is a file.
                ProcessFile(path)
            Else
                If Directory.Exists(path) Then
                    ' This path is a directory.
                    ProcessDirectory(path)
                Else
                    Console.WriteLine("{0} is not a valid file or directory.", path)
                End If
            End If
        Next path
    End Sub 'Main


    ' Process all files in the directory passed in, recurse on any directories 
    ' that are found, and process the files they contain.
    Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory.
        Dim fileName As String
        For Each fileName In fileEntries
            ProcessFile(fileName)

        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory.
        Dim subdirectory As String
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        Next subdirectory

    End Sub 'ProcessDirectory

    ' Insert logic for processing found files here.
    Public Shared Sub ProcessFile(ByVal path As String)
        Console.WriteLine("Processed file '{0}'.", path)
    End Sub 'ProcessFile
End Class 'RecursiveFileProcessor

備註

EnumerateFilesGetFiles方法的差異,如下所示:當您使用EnumerateFiles,您可以開始列舉的名稱集合,則會傳回整個集合之前,當您使用GetFiles,您必須等到整個陣列的名稱,才能存取陣列傳回。The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. 因此,當您使用許多檔案和目錄、EnumerateFiles可能會更有效率。Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

傳回的檔案名稱會附加至提供path參數。The returned file names are appended to the supplied path parameter.

這個方法相當於GetFiles(String, String)開頭是星號 (*) 指定為搜尋模式。This method is identical to GetFiles(String, String) with the asterisk (*) specified as the search pattern.

path參數可以指定相對或絕對路徑資訊。The path parameter can specify relative or absolute path information. 相對於目前工作目錄,會解譯相對路徑的資訊。Relative path information is interpreted as relative to the current working directory. 若要取得目前工作目錄,請參閱GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

不保證的順序傳回的檔案名稱;使用Sort是否需要特定的排序次序的方法。The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

path參數不區分大小寫。The path parameter is not case-sensitive.

如需一般 I/O 工作的清單,請參閱 < 一般 I/O 工作For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
目前的目錄路徑資訊存取。for access to path information for the current directory. 相關聯的列舉型別: PathDiscoveryAssociated enumeration: PathDiscovery

另請參閱

GetFiles(String, String) GetFiles(String, String) GetFiles(String, String) GetFiles(String, String)

傳回指定目錄中符合指定搜尋模式的檔案名稱 (包括檔案的路徑)。Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

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

參數

path
String String String String

要搜尋之目錄的相對或絕對路徑。The relative or absolute path to the directory to search. 這個字串不會區分大小寫。This string is not case-sensitive.

searchPattern
String String String String

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

傳回

String[]

指定目錄中,符合指定搜尋模式的檔案完整名稱 (包括路徑) 陣列,如果找不到任何檔案則為空陣列。An array of the full names (including paths) for the files in the specified directory that match the specified search pattern, or an empty array if no files are found.

例外狀況

path 為檔案名稱。path is a file name.

-或--or- 發生網路錯誤。A network error has occurred.

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

path 為零長度字串、只包含空格,或包含一或多個無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters. 您可以使用 GetInvalidPathChars() 查詢無效字元。You can query for invalid characters by using GetInvalidPathChars().

-或--or- searchPattern 不包含有效模式。searchPattern doesn't contain a valid pattern.

pathsearchPatternnullpath or searchPattern is null.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

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

範例

下列範例會計算指定的字母為開頭的檔案數目。The following example counts the number of files that begin with the specified letter.

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      
      // Only get files that begin with the letter "c".
      array<String^>^dirs = Directory::GetFiles( "c:\\", "c*" );
      Console::WriteLine( "The number of files starting with c is {0}.", dirs->Length );
      Collections::IEnumerator^ myEnum = dirs->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Console::WriteLine( myEnum->Current );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Only get files that begin with the letter "c".
            string[] dirs = Directory.GetFiles(@"c:\", "c*");
            Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
            foreach (string dir in dirs) 
            {
                Console.WriteLine(dir);
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            ' Only get files that begin with the letter "c".
            Dim dirs As String() = Directory.GetFiles("c:\", "c*")
            Console.WriteLine("The number of files starting with c is {0}.", dirs.Length)
            Dim dir As String
            For Each dir In dirs
                Console.WriteLine(dir)
            Next
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

備註

傳回的檔案名稱會附加至所提供path參數和傳回的檔案名稱的順序並不保證; 使用Sort是否需要特定的排序次序的方法。The returned file names are appended to the supplied path parameter and the order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

searchPattern 可以是常值與萬用字元字元的組合,但它不支援規則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. 允許使用下列萬用字元規範searchPatternThe 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. 比方說,searchPattern字串 」*t 」 中的所有名稱搜尋path結尾字母"t"。For example, the searchPattern string "*t" searches for all names in path 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 結尾不能兩個句點 ("...") 或包含兩個句點 ("...") 後面接著DirectorySeparatorCharAltDirectorySeparatorChar,也不能包含任何無效的字元。searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. 您可以使用 GetInvalidPathChars 方法查詢無效字元。You can query for invalid characters by using the GetInvalidPathChars method.

注意

當您使用星號萬用字元,在searchPattern這類 「*.txt 」,在指定的延伸模組中的字元數會影響搜尋,如下所示:When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:

  • 如果指定的延伸模組是剛好三個字元,則這個方法會傳回具有延伸模組可開始具有指定副檔名的檔案。If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. 例如,"*.xls"傳回"book.xls 」 和 「 book.xlsx"。For example, "*.xls" returns both "book.xls" and "book.xlsx".
  • 在其他情況下,這個方法會傳回完全符合指定的延伸模組的檔案。In all other cases, the method returns files that exactly match the specified extension. 例如,"*.ai"會傳回 「 file.ai",而不是 「 file.aif"。For example, "*.ai" returns "file.ai" but not "file.aif".

當您使用問號萬用字元時,則這個方法會傳回符合指定的副檔名的檔案。When you use the question mark wildcard character, this method returns only files that match the specified file extension. 例如,假設"file1.txt"和"file1.txtother 」,兩個檔案,在目錄中,搜尋模式的 「 檔案?。txt"會傳回只是第一個檔案,而搜尋模式的 「 檔案*.txt"會傳回這兩個檔案。For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.

注意

因為這個方法會檢查使用 8.3 檔案名稱格式和長時間的檔案名稱格式的檔案名稱,搜尋模式類似於 「*1*.txt 」 可能會傳回未預期的檔案名稱。Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. 例如,使用搜尋模式的 「*1*.txt"傳回"longfilename.txt",因為對等的 8.3 檔案名稱格式是 「 LONGFI~1.TXT"。For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".

EnumerateFilesGetFiles方法的差異,如下所示:當您使用EnumerateFiles,您可以開始列舉的名稱集合,則會傳回整個集合之前,當您使用GetFiles,您必須等到整個陣列的名稱,才能存取陣列傳回。The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. 因此,當您使用許多檔案和目錄、EnumerateFiles可能會更有效率。Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

path參數可以指定相對或絕對路徑資訊。The path parameter can specify relative or absolute path information. 相對於目前工作目錄,會解譯相對路徑的資訊。Relative path information is interpreted as relative to the current working directory. 若要取得目前工作目錄,請參閱GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

path參數不區分大小寫。The path parameter is not case-sensitive.

如需一般 I/O 工作的清單,請參閱 < 一般 I/O 工作For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
目前的目錄路徑資訊存取。for access to path information for the current directory. 相關聯的列舉型別: PathDiscoveryAssociated enumeration: PathDiscovery

另請參閱

GetFiles(String, String, EnumerationOptions) GetFiles(String, String, EnumerationOptions) GetFiles(String, String, EnumerationOptions)

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

參數

searchPattern
String String String String

傳回

String[]

GetFiles(String, String, SearchOption) GetFiles(String, String, SearchOption) GetFiles(String, String, SearchOption)

傳回指定目錄中符合指定搜尋模式的檔案名稱 (包括檔案的路徑),並使用值判斷是否搜尋子目錄。Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.

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

參數

path
String String String String

要搜尋之目錄的相對或絕對路徑。The relative or absolute path to the directory to search. 這個字串不會區分大小寫。This string is not case-sensitive.

searchPattern
String String String String

要比對 path 中檔案名稱的搜尋字串。The search string to match against the names of files in path. 這個參數可以包含有效常值路徑與萬用 (* 和 ?) 字元的組合,但是不支援規則運算式。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 all subdirectories or only the current directory.

傳回

String[]

指定目錄中,符合指定搜尋模式和選項的檔案完整名稱 (包括路徑) 陣列,如果找不到任何檔案則為空陣列。An array of the full names (including paths) for the files in the specified directory that match the specified search pattern and option, or an empty array if no files are found.

例外狀況

path 為零長度字串、只包含空格,或包含一或多個無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters. 您可以使用 GetInvalidPathChars() 方法查詢無效字元。You can query for invalid characters with the GetInvalidPathChars() method.

-或--or- searchPattern 不包含有效模式。searchPattern does not contain a valid pattern.

pathsearchpatternnullpath or searchpattern is null.

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

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

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

path 為檔案名稱。path is a file name.

-或--or- 發生網路錯誤。A network error has occurred.

備註

傳回的檔案名稱會附加至所提供的參數path並無法保證傳回的檔案名稱的順序; 請使用Sort是否需要特定的排序次序的方法。The returned file names are appended to the supplied parameter path and the order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

searchPattern 可以是常值與萬用字元字元的組合,但它不支援規則運算式。searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. 允許使用下列萬用字元規範searchPatternThe 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. 比方說,searchPattern字串 」*t 」 中的所有名稱搜尋path結尾字母"t"。For example, the searchPattern string "*t" searches for all names in path 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 結尾不能兩個句點 ("...") 或包含兩個句點 ("...") 後面接著DirectorySeparatorCharAltDirectorySeparatorChar,也不能包含任何無效的字元。searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. 您可以使用 GetInvalidPathChars 方法查詢無效字元。You can query for invalid characters by using the GetInvalidPathChars method.

注意

當您使用星號萬用字元,在searchPattern這類 「*.txt 」,在指定的延伸模組中的字元數會影響搜尋,如下所示:When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:

  • 如果指定的延伸模組是剛好三個字元,則這個方法會傳回具有延伸模組可開始具有指定副檔名的檔案。If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. 例如,"*.xls"傳回"book.xls 」 和 「 book.xlsx"。For example, "*.xls" returns both "book.xls" and "book.xlsx".
  • 在其他情況下,這個方法會傳回完全符合指定的延伸模組的檔案。In all other cases, the method returns files that exactly match the specified extension. 例如,"*.ai"會傳回 「 file.ai",而不是 「 file.aif"。For example, "*.ai" returns "file.ai" but not "file.aif".

當您使用問號萬用字元時,則這個方法會傳回符合指定的副檔名的檔案。When you use the question mark wildcard character, this method returns only files that match the specified file extension. 例如,假設"file1.txt"和"file1.txtother 」,兩個檔案,在目錄中,搜尋模式的 「 檔案?。txt"會傳回只是第一個檔案,而 「 file*.txt"的搜尋模式傳回這兩個檔案。For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.

注意

因為這個方法會檢查使用 8.3 檔案名稱格式和長時間的檔案名稱格式的檔案名稱,搜尋模式類似於 「*1*.txt 」 可能會傳回未預期的檔案名稱。Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. 例如,使用搜尋模式的 「*1*.txt"傳回"longfilename.txt",因為對等的 8.3 檔案名稱格式是 「 LONGFI~1.TXT"。For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".

EnumerateFilesGetFiles方法的差異,如下所示:當您使用EnumerateFiles,您可以開始列舉的名稱集合,則會傳回整個集合之前,當您使用GetFiles,您必須等到整個陣列的名稱,才能存取陣列傳回。The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. 因此,當您使用許多檔案和目錄、EnumerateFiles可能會更有效率。Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

檔案名稱中包含的完整路徑。The file names include the full path.

path參數可以指定相對或絕對路徑資訊。The path parameter can specify relative or absolute path information. 相對於目前工作目錄,會解譯相對路徑的資訊。Relative path information is interpreted as relative to the current working directory. 若要取得目前工作目錄,請參閱GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

path參數不區分大小寫。The path parameter is not case-sensitive.

如需一般 I/O 工作的清單,請參閱 < 一般 I/O 工作For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
目前的目錄路徑資訊存取。for access to path information for the current directory. 相關聯的列舉型別: PathDiscoveryAssociated enumeration: PathDiscovery

另請參閱

適用於