? 正好零個或一個字元。 萬用字元規範以外的字元則表示其本身。例如,searchPattern 字串「*t」會搜尋 path 中所有以字母「t」結束的名稱。searchPattern 字串「s*」則會搜尋 path 中所有以字母「s」開始的名稱。注意事項: 在 searchPattern 中使用星號萬用字元 (例如 "*.txt") 時,副檔名長度正好是三個字元時的比對行為,與副檔名長度大於或少於三個字元時不同。副檔名正好是三個字元的 searchPattern 會傳回副檔名包含三個以上字元的檔案,其中前三個字元符合 searchPattern 指定的副檔名。副檔名是一個、兩個或超過三個字元的 searchPattern 則只會傳回副檔名正好是該長度而且符合 searchPattern 所指定副檔名的檔案。當您使用問號萬用字元時,這個方法只會傳回符合指定之副檔名的檔案。例如,假設目錄中有兩個檔案 "file1.txt" 和 "file1.txtother",則 "file?.txt" 搜尋模式只會傳回第一個檔案,而 "file*.txt" 搜尋模式會傳回這兩個檔案。 下列清單顯示 searchPattern 參數在不同長度下的行為:"*.abc" 會傳回具有 .abc、.abcd、.abcde、.abcdef 等副檔名的檔案。"*.abcd" 只會傳回具有 .abcd 副檔名的檔案。"*.abcde" 只會傳回具有 .abcde 副檔名的檔案。"*.abcdef" 只會傳回具有 .abcdef 副檔名的檔案。注意事項: 由於這個方法會檢查具有 8.3 檔案名稱格式及長檔名格式的檔案名稱,所以類似於 "*1*.txt" 的搜尋模式可能會傳回未預期的檔案名稱。例如,使用 "*1*.txt" 的搜尋模式會傳回 "longfilename.txt",因為對等的 8.3 檔案名稱格式為 "LONGFI~1.TXT"。 允許用 path 參數來指定相對或絕對路徑的資訊。相對路徑資訊會被解譯為與目前的工作目錄相對。若要取得目前的工作目錄,請參閱 GetCurrentDirectory。path 參數是不區分大小寫的。如需一般 I/O 工作的清單,請參閱一般 I/O 工作。 範例 下列程式碼範例會計算以指定字母開頭的檔案數目。Visual Basic 複製程式碼 Imports System Imports System.IOPublic 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 C# 複製程式碼 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()); } } }
命名空間: System.IO
組件: mscorlib (在 mscorlib.dll 中)
語法
Visual Basic (宣告)
Public Shared Function GetFiles ( _
path As String, _
searchPattern As String _
) As String()Visual Basic (使用方式)
Dim path As String
Dim searchPattern As String
Dim returnValue As String()returnValue = Directory.GetFiles(path, _
searchPattern)C#
public static string[] GetFiles(
string path,
string searchPattern
)Visual C++
public:
static array<String^>^ GetFiles(
String^ path,
String^ searchPattern
)J#
public static String[] GetFiles(
String path,
String searchPattern
)JScript
public static function GetFiles(
path : String,
searchPattern : String
) : String[]
參數
path
型別:System..::.String
要搜尋的目錄。searchPattern
型別:System..::.String
要比對 path 中檔案名稱的搜尋字串。參數既不可以使用兩個句點 (..) 做為結尾,或者含有兩個句點 (..) 後面再接著 DirectorySeparatorChar 或 AltDirectorySeparatorChar,也不可以包含 InvalidPathChars 中的任何字元。傳回值
型別:array<System..::.String>[]()[]
含有指定目錄 (符合指定搜尋模式) 中檔案的名稱的 String 陣列。 例外狀況
例外狀況 條件
UnauthorizedAccessException 呼叫端沒有所要求的使用權限。
ArgumentException path 是長度為零的字串、只包含泛空白字元,或包含一或多個無效的字元 (如 InvalidPathChars 所定義)。-或-searchPattern 不包含有效模式。
ArgumentNullException path 或 searchPattern 是 nullNothingnullptrNull 參照 (即 Visual Basic 中的 Nothing)。
PathTooLongException 指定的路徑、檔案名稱或兩者都超過系統定義的最大長度。例如:在 Windows 平台上,路徑必須小於 248 個字元,而檔案名稱必須小於 260 個字元。
DirectoryNotFoundException 指定的路徑無效 (例如,位於未對應的磁碟上)。
備註
傳回的檔案名稱會附加到提供的 path 參數。如果沒有檔案,或是沒有任何檔案符合 searchPattern參數,則此方法會傳回空白陣列。不保證傳回之檔案名稱的次序;如果需要特定的排序次序,請使用 Sort()()() 方法。下列萬用字元規範在 searchPattern 中受允許。萬用字元
說明
*
零個或更多字元。
?
正好零個或一個字元。
萬用字元規範以外的字元則表示其本身。例如,searchPattern 字串「*t」會搜尋 path 中所有以字母「t」結束的名稱。searchPattern 字串「s*」則會搜尋 path 中所有以字母「s」開始的名稱。注意事項:
在 searchPattern 中使用星號萬用字元 (例如 "*.txt") 時,副檔名長度正好是三個字元時的比對行為,與副檔名長度大於或少於三個字元時不同。副檔名正好是三個字元的 searchPattern 會傳回副檔名包含三個以上字元的檔案,其中前三個字元符合 searchPattern 指定的副檔名。副檔名是一個、兩個或超過三個字元的 searchPattern 則只會傳回副檔名正好是該長度而且符合 searchPattern 所指定副檔名的檔案。當您使用問號萬用字元時,這個方法只會傳回符合指定之副檔名的檔案。例如,假設目錄中有兩個檔案 "file1.txt" 和 "file1.txtother",則 "file?.txt" 搜尋模式只會傳回第一個檔案,而 "file*.txt" 搜尋模式會傳回這兩個檔案。
下列清單顯示 searchPattern 參數在不同長度下的行為:"*.abc" 會傳回具有 .abc、.abcd、.abcde、.abcdef 等副檔名的檔案。"*.abcd" 只會傳回具有 .abcd 副檔名的檔案。"*.abcde" 只會傳回具有 .abcde 副檔名的檔案。"*.abcdef" 只會傳回具有 .abcdef 副檔名的檔案。注意事項:
由於這個方法會檢查具有 8.3 檔案名稱格式及長檔名格式的檔案名稱,所以類似於 "*1*.txt" 的搜尋模式可能會傳回未預期的檔案名稱。例如,使用 "*1*.txt" 的搜尋模式會傳回 "longfilename.txt",因為對等的 8.3 檔案名稱格式為 "LONGFI~1.TXT"。
允許用 path 參數來指定相對或絕對路徑的資訊。相對路徑資訊會被解譯為與目前的工作目錄相對。若要取得目前的工作目錄,請參閱 GetCurrentDirectory。path 參數是不區分大小寫的。如需一般 I/O 工作的清單,請參閱一般 I/O 工作。 範例
下列程式碼範例會計算以指定字母開頭的檔案數目。Visual Basic 複製程式碼
Imports System
Imports System.IOPublic 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
C# 複製程式碼
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());
}
}
}
2.对每个分区用DirectoryInfo 类里面的GetDirectories 方法和GetFiles方法递归返回所有文件名,对比找到你所需要的.
3.这时候你已经知道文件的路径了,判断一下就能找到盘符了吧