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

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

PHPz
PHPz轉載
2023-08-29 21:37:061527瀏覽

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.com。如有侵權,請聯絡admin@php.cn刪除