搜尋
首頁後端開發C#.Net教程C# 程式取得目錄中存在的所有文件

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); 

在這裡,searchOption是一個參數,它指示搜尋是否應該只包含目前路徑或所有子目錄。 searchPattern是與使用者定義路徑中檔案名稱相符的搜尋字串。它可以包含有效的文字路徑和通配符(*和?),但不支援正規表示式。

演算法

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中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
C#.NET生態系統:框架,庫和工具C#.NET生態系統:框架,庫和工具Apr 24, 2025 am 12:02 AM

C#.NET生態系統提供了豐富的框架和庫,幫助開發者高效構建應用。 1.ASP.NETCore用於構建高性能Web應用,2.EntityFrameworkCore用於數據庫操作。通過理解這些工具的使用和最佳實踐,開發者可以提高應用的質量和性能。

將C#.NET應用程序部署到Azure/AWS:逐步指南將C#.NET應用程序部署到Azure/AWS:逐步指南Apr 23, 2025 am 12:06 AM

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

C#.NET:強大的編程語言簡介C#.NET:強大的編程語言簡介Apr 22, 2025 am 12:04 AM

C#和.NET的結合為開發者提供了強大的編程環境。 1)C#支持多態性和異步編程,2).NET提供跨平台能力和並發處理機制,這使得它們在桌面、Web和移動應用開發中廣泛應用。

.NET框架與C#:解碼術語.NET框架與C#:解碼術語Apr 21, 2025 am 12:05 AM

.NETFramework是一個軟件框架,C#是一種編程語言。 1..NETFramework提供庫和服務,支持桌面、Web和移動應用開發。 2.C#設計用於.NETFramework,支持現代編程功能。 3..NETFramework通過CLR管理代碼執行,C#代碼編譯成IL後由CLR運行。 4.使用.NETFramework可快速開發應用,C#提供如LINQ的高級功能。 5.常見錯誤包括類型轉換和異步編程死鎖,調試需用VisualStudio工具。

揭開c#.net的神秘面紗:初學者的概述揭開c#.net的神秘面紗:初學者的概述Apr 20, 2025 am 12:11 AM

C#是一種由微軟開發的現代、面向對象的編程語言,.NET是微軟提供的開發框架。 C#結合了C 的性能和Java的簡潔性,適用於構建各種應用程序。 .NET框架支持多種語言,提供垃圾回收機制,簡化內存管理。

C#和.NET運行時:它們如何一起工作C#和.NET運行時:它們如何一起工作Apr 19, 2025 am 12:04 AM

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

C#.NET開發:入門的初學者指南C#.NET開發:入門的初學者指南Apr 18, 2025 am 12:17 AM

要開始C#.NET開發,你需要:1.了解C#的基礎知識和.NET框架的核心概念;2.掌握變量、數據類型、控制結構、函數和類的基本概念;3.學習C#的高級特性,如LINQ和異步編程;4.熟悉常見錯誤的調試技巧和性能優化方法。通過這些步驟,你可以逐步深入C#.NET的世界,並編寫高效的應用程序。

c#和.net:了解兩者之間的關係c#和.net:了解兩者之間的關係Apr 17, 2025 am 12:07 AM

C#和.NET的關係是密不可分的,但它們不是一回事。 C#是一門編程語言,而.NET是一個開發平台。 C#用於編寫代碼,編譯成.NET的中間語言(IL),由.NET運行時(CLR)執行。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),