目錄
Introduction
1. GetFiles() 方法
演算法
Example
輸出
Algorithm for SearchOption.AllDirectories
2. EnumerateFiles方法
输出
3. Directory.GetFileSystemEntries() Method
Algorithm
Conclusion
首頁 後端開發 C#.Net教程 C# 程式取得目錄中存在的所有文件

C# 程式取得目錄中存在的所有文件

Aug 29, 2023 pm 09:37 PM

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

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱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

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

C語言各種符號的使用方法 C語言各種符號的使用方法 Apr 03, 2025 pm 04:48 PM

C 語言中符號的使用方法涵蓋算術、賦值、條件、邏輯、位運算符等。算術運算符用於基本數學運算,賦值運算符用於賦值和加減乘除賦值,條件運算符用於根據條件執行不同操作,邏輯運算符用於邏輯操作,位運算符用於位級操作,特殊常量用於表示空指針、文件結束標記和非數字值。

char在C語言字符串中的作用是什麼 char在C語言字符串中的作用是什麼 Apr 03, 2025 pm 03:15 PM

在 C 語言中,char 類型在字符串中用於:1. 存儲單個字符;2. 使用數組表示字符串並以 null 終止符結束;3. 通過字符串操作函數進行操作;4. 從鍵盤讀取或輸出字符串。

char在C語言中如何處理特殊字符 char在C語言中如何處理特殊字符 Apr 03, 2025 pm 03:18 PM

C語言中通過轉義序列處理特殊字符,如:\n表示換行符。 \t表示製表符。使用轉義序列或字符常量表示特殊字符,如char c = '\n'。注意,反斜杠需要轉義兩次。不同平台和編譯器可能有不同的轉義序列,請查閱文檔。

c#多線程和異步的區別 c#多線程和異步的區別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

char與wchar_t在C語言中的區別 char與wchar_t在C語言中的區別 Apr 03, 2025 pm 03:09 PM

在 C 語言中,char 和 wchar_t 的主要區別在於字符編碼:char 使用 ASCII 或擴展 ASCII,wchar_t 使用 Unicode;char 佔用 1-2 個字節,wchar_t 佔用 2-4 個字節;char 適用於英語文本,wchar_t 適用於多語言文本;char 廣泛支持,wchar_t 依賴於編譯器和操作系統是否支持 Unicode;char 的字符範圍受限,wchar_t 的字符範圍更大,並使用專門的函數進行算術運算。

char在C語言中如何進行類型轉換 char在C語言中如何進行類型轉換 Apr 03, 2025 pm 03:21 PM

在 C 語言中,char 類型轉換可以通過:強制類型轉換:使用強制類型轉換符將一種類型的數據直接轉換為另一種類型。自動類型轉換:當一種類型的數據可以容納另一種類型的值時,編譯器自動進行轉換。

char數組在C語言中如何使用 char數組在C語言中如何使用 Apr 03, 2025 pm 03:24 PM

char 數組在 C 語言中存儲字符序列,聲明為 char array_name[size]。訪問元素通過下標運算符,元素以空終止符 '\0' 結尾,用於表示字符串終點。 C 語言提供多種字符串操作函數,如 strlen()、strcpy()、strcat() 和 strcmp()。

char和unsigned char的區別是什麼 char和unsigned char的區別是什麼 Apr 03, 2025 pm 03:36 PM

char 和 unsigned char 是存儲字符數據的兩種數據類型,主要區別在於處理負數和正數的方式:值範圍:char 有符號 (-128 到 127),unsigned char 無符號 (0 到 255)。負數處理:char 可以存儲負數,unsigned char 不能。位模式:char 最高位表示符號,unsigned char 無符號位。算術運算:char 和 unsigned char 作為有符號和無符號類型,其算術運算方式不同。兼容性:char 和 unsigned char

See all articles