.NET HELP C# Directory.GetFiles (How It Works: A Guide for Developers) Jacob Mellor 更新:2025年6月22日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 C# out 參數。 透過將此功能與 IronPDF 搭配使用,開發人員便可大規模地自動化 PDF 工作流程。 例如,您可以使用 Directory.GetFiles 來找出資料夾中的所有 PDF 檔案,然後再使用 IronPDF 來大量處理這些檔案,以執行合併、新增註解或產生報表等工作。 這樣的組合可以簡化操作,尤其是在處理檔案系統中的許多檔案時。 什麼是 IronPDF? IronPDF 是一個強大的 .NET 函式庫,提供開發人員與 PDF 檔案無縫接軌的工具。 使用 IronPDF,您可以使用直接、直觀的方法來建立、編輯、合併、分割和處理 PDF。 它包括強大的功能,例如 HTML-to-PDF轉換、進階樣式設定和元資料處理。 對於從事需要 PDF 處理的應用程式的 .NET 開發人員而言,IronPDF 是一種無價的工具,可簡化工作流程並提高生產力。 開始 安裝 IronPDF NuGet 套件安裝 首先,透過 NuGet 將 IronPDF 加入您的專案: 1.在 Visual Studio 中開啟您的專案。 2.移至 Tools 功能表,並選擇 NuGet Package Manager > Manage NuGet Packages for Solution。 3.在 NuGet 套件管理器中搜尋 IronPDF。 4.安裝最新版本的 IronPdf。 或者使用 NuGet Package Manager Console: Install-Package IronPdf C# 中 Directory.GetFiles 的基本原理; Directory.GetFiles 方法是 System.IO 命名空間的一部分,用於從檔案系統擷取檔案名稱。 此方法是 Directory 類別的公共靜態字串成員,可簡化檔案路徑的存取。 舉例來說 string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf"); string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf"); $vbLabelText $csharpLabel 此片段擷取目前目錄中的所有 PDF 檔案。 透過將此方法與 IronPDF 結合,您可以建立一次處理多個檔案的自動化解決方案。 您也可以套用指定的搜尋模式 (定義為字串模式),根據檔案的副檔名或名稱過濾檔案。 您可以透過指定搜尋選項 (例如包含搜尋子目錄) 來進一步精細檔案擷取邏輯: string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf", SearchOption.AllDirectories); string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf", SearchOption.AllDirectories); $vbLabelText $csharpLabel 這可確保嵌套資料夾中的檔案也包含在內,擷取每個檔案的絕對路徑,並使此方法適用於各種情況。 實用案例 從目錄擷取和處理 PDF 檔案 範例:載入所有 PDF 檔案進行處理 使用 Directory.GetFiles 可以遍歷目錄中的所有 PDF 檔案,並使用 IronPDF 處理它們: string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf"); foreach (string file in pdfFiles) { // Load the PDF with IronPDF var pdf = PdfDocument.FromFile(file); Console.WriteLine($"Processing file: {Path.GetFileName(file)}"); } string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf"); foreach (string file in pdfFiles) { // Load the PDF with IronPDF var pdf = PdfDocument.FromFile(file); Console.WriteLine($"Processing file: {Path.GetFileName(file)}"); } $vbLabelText $csharpLabel 本範例示範如何從目錄載入多個 PDF 檔案進行處理。 載入後,您可以執行各種作業,例如擷取文字、新增註解或根據內容產生新的 PDF。 使用搜尋模式過濾檔案 範例:依名稱或日期選擇 PDF 您可以將 Directory.GetFiles 與 LINQ 結合,根據建立或修改日期等條件來篩選檔案: string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf"); var recentFiles = pdfFiles.Where(file => File.GetLastWriteTime(file) > DateTime.Now.AddDays(-7)); foreach (string file in recentFiles) { Console.WriteLine($"Recent file: {Path.GetFileName(file)}"); } string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf"); var recentFiles = pdfFiles.Where(file => File.GetLastWriteTime(file) > DateTime.Now.AddDays(-7)); foreach (string file in recentFiles) { Console.WriteLine($"Recent file: {Path.GetFileName(file)}"); } $vbLabelText $csharpLabel 此方法可確保只處理相關檔案,節省時間和計算資源。 例如,您可以使用此方法僅處理最近一週內產生的最新發票或報告。 使用 IronPDF 和 Directory.GetFiles 進行批次作業 範例:追加多個 PDF 文件 您可以將一個目錄中的多個 PDF 附加到單一檔案中: string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf"); var pdfAppend = new PdfDocument(200, 200); foreach (string file in pdfFiles) { var pdf = PdfDocument.FromFile(file); pdfAppend.AppendPdf(pdf); } pdfAppend.SaveAs("LargePdf.pdf"); Console.WriteLine("PDFs Appended successfully!"); string[] pdfFiles = Directory.GetFiles("C:\\Documents\\PDFs", "*.pdf"); var pdfAppend = new PdfDocument(200, 200); foreach (string file in pdfFiles) { var pdf = PdfDocument.FromFile(file); pdfAppend.AppendPdf(pdf); } pdfAppend.SaveAs("LargePdf.pdf"); Console.WriteLine("PDFs Appended successfully!"); $vbLabelText $csharpLabel 這種方法對於建立綜合報告、歸檔多份文件或準備簡報特別有用。 透過自動化此流程,您可以毫不費力地處理大量的檔案集合。 逐步實施 設定專案 程式碼片段:初始化 IronPDF 並處理 PDF 檔案 以下程式碼示範 IronPDF 如何與 Directory.GetFiles 搭配使用,以載入並處理 PDF 文件。 using IronPdf; using System; using System.IO; class Program { static void Main() { // Retrieve all PDF file paths from the specified directory string[] pdfFiles = Directory.GetFiles("C:\\Users\\kyess\\Documents\\PDFs", "*.pdf"); // Initialize a PdfDocument var pdfAppend = new PdfDocument(200, 200); // Create a text annotation to add to each PDF TextAnnotation annotation = new TextAnnotation(0) { Contents = "Processed by IronPDF", X = 50, Y = 50, }; // Iterate over each file path, load, annotate, and save foreach (string file in pdfFiles) { var pdf = PdfDocument.FromFile(file); pdf.Annotations.Add(annotation); pdf.SaveAs(file); } } } using IronPdf; using System; using System.IO; class Program { static void Main() { // Retrieve all PDF file paths from the specified directory string[] pdfFiles = Directory.GetFiles("C:\\Users\\kyess\\Documents\\PDFs", "*.pdf"); // Initialize a PdfDocument var pdfAppend = new PdfDocument(200, 200); // Create a text annotation to add to each PDF TextAnnotation annotation = new TextAnnotation(0) { Contents = "Processed by IronPDF", X = 50, Y = 50, }; // Iterate over each file path, load, annotate, and save foreach (string file in pdfFiles) { var pdf = PdfDocument.FromFile(file); pdf.Annotations.Add(annotation); pdf.SaveAs(file); } } } $vbLabelText $csharpLabel 控制台輸出 說明 本代碼示範如何使用 C# 中的 IronPDF 為指定目錄中的所有 PDF 檔案新增文字註釋。 程式一開始會使用 Directory.GetFiles 方法從所提供的資料夾中擷取所有 PDF 檔案路徑,此方法依賴字串路徑來指定目錄,並支援依檔案副檔名過濾,會傳回包含所有副檔名為 ".pdf" 的 PDF 檔案路徑的字串檔案名陣列。 接下來,程式碼會初始化一個尺寸為 200x200 的 PdfDocument 物件 (pdfAppend),不過這個特定的實例不會直接用在循環中。然後,程式會建立一個 TextAnnotation 物件,並將文字"Processed by IronPdf"置於座標 (50, 50)。 此註解將被添加到每個 PDF 檔案中。 在 foreach 環路中,程式會遍歷 pdfFiles 陣列中的每個檔案路徑。 對於每個檔案,它會使用 PdfDocument.FromFile(file) 載入 PDF,將先前建立的註解新增至 PDF 的 Annotations 集合,然後再使用 pdf.SaveAs(file) 將更新後的 PDF 儲存回絕對路徑。 此流程可確保指定目錄中的每個 PDF 都會收到相同的註解,並在儲存時包含註解。 效能提示與最佳實務 使用 Directory.GetFiles 優化檔案擷取 使用像 Directory.EnumerateFiles 之類的異步方法,可以在使用大型目錄時獲得更好的效能。 有效管理大量檔案 以較小的批次處理檔案,以減少記憶體消耗: foreach (var batch in pdfFiles.Batch(10)) { foreach (string file in batch) { var pdf = PdfDocument.FromFile(file); // Process PDF } } foreach (var batch in pdfFiles.Batch(10)) { foreach (string file in batch) { var pdf = PdfDocument.FromFile(file); // Process PDF } } $vbLabelText $csharpLabel 檔案處理和 PDF 生成中的錯誤處理 將檔案處理包裝在 try-catch 區塊中以處理異常: try { var pdf = PdfDocument.FromFile(file); // Process PDF } catch (Exception ex) { Console.WriteLine($"Error processing {file}: {ex.Message}"); } try { var pdf = PdfDocument.FromFile(file); // Process PDF } catch (Exception ex) { Console.WriteLine($"Error processing {file}: {ex.Message}"); } $vbLabelText $csharpLabel 結論 結合 Directory.GetFiles 與 IronPDF 的強大功能,可讓開發人員有效率地管理和處理 PDF 檔案。 有了這種方法,批次處理、合併、篩選和轉換 PDF 等工作都變得無縫,大幅減少手動工作並提高生產力。 透過利用 IronPdf 的進階功能(包括新增標題、元資料和樣式設定),開發人員可以根據他們的需求量身打造高品質的專業 PDF 文件。 在本指南中,我們探討了如何使用 Directory.GetFiles 來擷取和處理 IronPDF 的 PDF 檔案。 從設定專案到實作實例,我們涵蓋了可應用於實際情境的基本技巧。 無論您是致力於自動化文件工作流程,或是增強 .NET 應用程式的功能,這個組合都能提供強大且可擴充的解決方案。 如果您準備深入了解 IronPDF 並探索進階功能,請考慮參考 官方文件,讓您可以在自己的專案中測試該函式庫。 常見問題解答 Directory.GetFiles 方法在 C# 中如何運作? C# 中的 Directory.GetFiles 方法是 System.IO 命名空間的一部分,可讓開發人員從指定目錄擷取檔案路徑。它支援包含子目錄的搜尋模式和選項,使其能有效率地存取特定的檔案類型或名稱。 如何使用 C# 自動處理 PDF 檔案? 您可以在 Directory.GetFiles 方法旁使用 IronPDF 在 C# 中自動處理 PDF 檔案。這可讓您定位目錄中的 PDF 檔案,並執行合併、新增註解或自動產生報告等工作。 結合 Directory.GetFiles 與 PDF 函式庫有什麼好處? 結合 Directory.GetFiles 與 IronPDF 等 PDF 函式庫,可以自動且有效率地管理 PDF 文件。您可以大量取得並處理 PDF、套用修改並整合檔案,以提高生產力並減少手動工作。 如何使用 C# 將多個 PDF 附加到單一文件中? 若要將多個 PDF 附加到單一文件中,請使用 Directory.GetFiles 擷取目錄中的所有 PDF 檔案。然後,使用 IronPDF 載入每個 PDF,並將它們追加到單一的 PdfDocument 物件中,即可儲存為整合後的 PDF 檔案。 如何在 C# 中根據創建日期過濾目錄檔案? 您可以使用 LINQ with Directory.GetFiles 依據建立日期篩選目錄檔案。例如,若要選擇在上星期內建立的檔案,請使用:var recentFiles = pdfFiles.Where(file => File.GetCreationTime(file) > DateTime.Now.AddDays(-7)); 在 C# 中處理大量檔案的最佳做法是什麼? 若要處理大量檔案,請使用 Directory.EnumerateFiles 等異步方法,以減少擷取時間來提高效能。這對於有效率地處理大型目錄尤其有用。 如何在 C# 中處理 PDF 檔案時處理錯誤? 在 PDF 檔案處理過程中,以 try-catch 區塊包覆作業來處理錯誤。這可確保優雅地管理異常,允許應用程式繼續執行,而不會因意外錯誤而當機。 使用 C# 中的 PDF 函式庫進行批次處理的範例是什麼? 批量處理的一個範例是使用 Directory.GetFiles 擷取 PDF 檔案,然後運用 IronPDF 來批量合併或批注 PDF 檔案。此方法可將重複性工作自動化,節省時間與精力。 如何使用 .NET 函式庫將文字註解新增至 PDF? 若要使用 IronPDF 為 PDF 新增文字註解,請建立一個具有指定內容和位置的 TextAnnotation 物件。載入每個 PDF,將註解新增至其 Annotations 集合,並儲存更新後的文件。 在 Visual Studio 中透過 NuGet 安裝 PDF 函式庫的步驟為何? 若要在 Visual Studio 中透過 NuGet 安裝 PDF 函式庫,請開啟專案,導覽至「工具」>「NuGet Package Manager」>「Manage NuGet Packages for Solution」,搜尋 IronPDF,並進行安裝。或者,使用 NuGet Package Manager Console 指令:Install-Package IronPdf。 Jacob Mellor 立即與工程團隊聊天 首席技術長 Jacob Mellor 是 Iron Software 的首席技術長,也是開創 C# PDF 技術的有遠見的工程師。作為 Iron Software 核心程式碼庫背後的原始開發人員,他從公司成立之初就塑造了公司的產品架構,與首席執行官 Cameron Rimington 一起將公司轉型為一家 50 多人的公司,為 NASA、Tesla 和全球政府機構提供服務。Jacob 持有曼徹斯特大學土木工程一級榮譽工程學士學位 (BEng)(1998-2001 年)。Jacob 於 1999 年在倫敦開設了他的第一家軟體公司,並於 2005 年創建了他的第一個 .NET 元件,之後,他專門解決微軟生態系統中的複雜問題。他的旗艦產品 IronPDF & Iron Suite for .NET 函式庫在全球的 NuGet 安裝量已超過 3000 萬次,他的基礎程式碼持續為全球使用的開發人員工具提供動力。Jacob 擁有 25 年的商業經驗和 41 年的編碼專業知識,他一直專注於推動企業級 C#、Java 和 Python PDF 技術的創新,同時指導下一代的技術領導者。 相關文章 更新2025年12月11日 Bridging CLI Simplicity & .NET : Using Curl DotNet with IronPDF Jacob Mellor has bridged this gap with CurlDotNet, a library created to bring the familiarity of cURL to the .NET ecosystem. 閱讀更多 更新2025年12月20日 RandomNumberGenerator C# Using the RandomNumberGenerator C# class can help take your PDF generation and editing projects to the next level 閱讀更多 更新2025年12月20日 C# String Equals (How it Works for Developers) When combined with a powerful PDF library like IronPDF, switch pattern matching allows you to build smarter, cleaner logic for document processing 閱讀更多 C# Convert String to Bubble (How it Works for Developers)C# Out Parameter (How It Works: A G...
更新2025年12月11日 Bridging CLI Simplicity & .NET : Using Curl DotNet with IronPDF Jacob Mellor has bridged this gap with CurlDotNet, a library created to bring the familiarity of cURL to the .NET ecosystem. 閱讀更多
更新2025年12月20日 RandomNumberGenerator C# Using the RandomNumberGenerator C# class can help take your PDF generation and editing projects to the next level 閱讀更多
更新2025年12月20日 C# String Equals (How it Works for Developers) When combined with a powerful PDF library like IronPDF, switch pattern matching allows you to build smarter, cleaner logic for document processing 閱讀更多