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.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


    ' 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

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

備註

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. 若要取得目前的工作目錄, GetCurrentDirectory請參閱。To obtain the current working directory, see GetCurrentDirectory.

不保證所傳回檔案名的順序;如果需要特定的排序次序, 請使用方法。SortThe 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.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. 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. 例如, searchPattern字串 "*t" 會path搜尋結尾為字母 "t" 的所有名稱。For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". 字串 "s*" 會path搜尋開頭為字母 "s" 的所有名稱。 searchPatternThe searchPattern string "s*" searches for all names in path beginning with the letter "s".

searchPattern不能在兩個句點 ("..") 中結束, 或包含兩個句號 ("..." DirectorySeparatorChar ) AltDirectorySeparatorChar, 後面接著或, 也不能包含任何不正確字元。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" 和 "file1. txtother", 在目錄中, "file?" 的搜尋模式。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", 因為對等的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. 若要取得目前的工作目錄, GetCurrentDirectory請參閱。To 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. 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. 例如, searchPattern字串 "*t" 會path搜尋結尾為字母 "t" 的所有名稱。For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". 字串 "s*" 會path搜尋開頭為字母 "s" 的所有名稱。 searchPatternThe searchPattern string "s*" searches for all names in path beginning with the letter "s".

searchPattern不能在兩個句點 ("..") 中結束, 或包含兩個句號 ("..." DirectorySeparatorChar ) AltDirectorySeparatorChar, 後面接著或, 也不能包含任何不正確字元。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" 和 "file1. txtother", 在目錄中, "file?" 的搜尋模式。txt "只會傳回第一個檔案, 而" file* "的搜尋模式則會傳回這兩個檔案。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", 因為對等的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. 若要取得目前的工作目錄, GetCurrentDirectory請參閱。To 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

另請參閱

適用於