C# 程式取得目錄中存在的所有文件
Introduction
On the computer, we can store files in a directory, also known as a folder. A directory also contains shortcuts to other directories and files. If we want to know what all files have stored in a directory then C# also offers an easy way to do so. In this article, we will be learning a C# program to get all files present in a directory.
There are more than a couple of ways to know the files available in the directory. Let us discuss them in the upcoming sections.
1. GetFiles() 方法
為了知道指定目錄中存在的檔案的名稱,我們使用GetFiles()方法。
public static string[] GetFiles (string path);
We can use GetFiles() and GetDirectories() to know the files and subdirectories in the specified directory.
其參數即目錄的絕對或相對路徑是一個字串。並且它是大小寫不敏感的。此函數傳回一個包含指定目錄中檔案名稱及其路徑的清單的陣列。當目錄為空時,也會傳回一個空數組。
演算法
現在,讓我們討論使用GetFiles()方法來取得目錄中所有檔案的演算法。
第一步 - 首先,我們宣告一個字串來儲存目錄的位址。
Step 2 − We get the list of the files by using GetFiles() and store it in an array named fyles.
Step 3 − Finally, we print the list of files.
#Example
using System; using System.IO; public class Example { public static void Main() { string directloc = @"D:\mypc\addre"; // files list from the root directory and prints it string[] fyles = Directory.GetFiles(directloc); Console.WriteLine(String.Join(Environment.NewLine, fyles)); } }
輸出
abrew.txt zuma.txt
Now, to get the details of the types of files that are present in the root directory i.e., the directory we are searching and its subfolders then we use the '*' pattern and SearchOption.AllDirector subfolders then we use the '*' pattern and SearchOption.AllDirectories retriple the mulsfiles available in the directory and its subdirectories.
Algorithm for SearchOption.AllDirectories
Now, let’s discuss the algorithm to get all the files present in a directory and its subdirectories using SearchOption.AllDirectories method.
步驟 1 − 首先,我們宣告一個字串來儲存目錄的位址。
Step 2 − We get the list of the files in the directory and its subdirectories by using SearchOption.AllDirectories and store it in an array named fyles.
Step 3 − Finally, we print the list of files.
#Example
using System; using System.IO; public class Example { public static void Main() { string directloc = @"D:\mypc\addre"; // files list from the root directory and its subdirectories and prints it string[] fyles = Directory.GetFiles(directloc, "*", SearchOption.AllDirectories); Console.WriteLine(String.Join(Environment.NewLine, fyles)); } }
輸出
abrew.txt zuma.txt
所以,透過使用GetFiles()方法,我們可以了解目錄及其子目錄中的檔案。現在,我們轉向下一節,討論EnumerateFiles方法以了解目錄及其子目錄中的檔案。
2. EnumerateFiles方法
From the method's name, we can say that this is an enumerable collection returning method. So, this method returns an enumerable collection of complev #
public static System.Collections.Generic.IEnumerable EnumerateFiles (string path, string searchPattern, System.IO.SearchOption searchOption);
演算法
Now, let’s discuss the algorithm to get all the files present in a directory using Directory.EnumerateFiles() method.
步驟 1 − 首先,我們宣告一個字串來儲存目錄的位址。
第二步驟 − 我們透過使用Directory.EnumerateFiles(directloc, "*", SearchOption.AllDirectories)取得目錄及其子目錄中的檔案列表,並將其儲存在名為fyles的變數中。
Step 3 − Finally, we print the list of files.#
Example
using System; using System.IO; using System.Collections.Generic; public class Example { public static void Main() { string directloc = @"D:\mypc\addre"; // files list from the root directory and its subdirectories and prints it var fyles = Directory.EnumerateFiles(directloc, "*", SearchOption.AllDirectories); Console.WriteLine(String.Join(Environment.NewLine, fyles)); } }
输出
abrew.txt zuma.txt
在这个方法中,searchPattern非常重要,因为它是通配符和字面字符的混合。它不允许使用正则表达式。以下是通配符和它们的匹配项。
Wildcard Specifier |
Matches | 的中文翻译为:匹配 |
---|---|---|
*(星号) |
Zero or more characters in that position |
|
?(question mark) |
Exactly one character in that position |
If we use '*o' then each file name is checked to end with o. And if we use 'a*' then each file name is checked to start with a. Also when the asterisk wildcard character is used in searchPattern and the name of a three-character file extension, such as "*.txt," this returns files with extensions that have with the stated extension. Now, let’s see another method.
3. Directory.GetFileSystemEntries() Method
This method returns the names of all files and subdirectories that meet the conditions given by the programmer. The syntax for this method is as follows.
public static string[] GetFileSystemEntries (string path);
另一个选择是利用 Directory。GetFileSystemEntries() 方法检索提供路径中所有文件和子目录的名称。它可以使用搜索模式和搜索选项进行重载。当提供了搜索模式时,该方法将其与路径中的文件和文件夹名称进行比较。如果使用了 SearchOption.AllDirectories 选项,它将搜索所有子目录。
Algorithm
现在,让我们讨论使用 Directory.GetFileSystemEntries() 方法获取目录中所有文件的算法。
步骤 1 − 首先,我们声明一个字符串来存储目录的地址。
第二步 − 通过使用Directory.GetFileSystemEntries(rootdir, "*", SearchOption.AllDirectories)获取目录及其子目录中的文件列表,并将其存储在一个数组中。
Step 3 − Finally, we print the list of files.
Example
using System; using System.IO; public class Example { public static void Main() { string directloc = @"D:\mypc\addre"; // files list from the root directory and its subdirectories and prints it string[] fyles = Directory.GetFileSystemEntries(directloc, "*", SearchOption.AllDirectories); Console.WriteLine(String.Join(Environment.NewLine, fyles)); } }
输出
abrew.txt zuma.txt
Conclusion
所以,这篇文章就到这里了。在这篇文章中,我们学习了如何编写一个C#程序来获取目录中的所有文件。我们讨论了不同的方法来实现这个目标。我们还了解了这些方法的算法,并学习了它们的代码。希望这篇文章能够增加你对C#的知识。
以上是C# 程式取得目錄中存在的所有文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

C#和.NET運行時緊密合作,賦予開發者高效、強大且跨平台的開發能力。 1)C#是一種類型安全且面向對象的編程語言,旨在與.NET框架無縫集成。 2).NET運行時管理C#代碼的執行,提供垃圾回收、類型安全等服務,確保高效和跨平台運行。
