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:mypcaddre";      // files list from the root directory and prints it      string[] fyles = Directory.GetFiles(directloc);      Console.WriteLine(String.Join(Environment.NewLine, fyles));   }}

输出

abrew.txtzuma.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.AllDirectories which retrieve the multiple types of files 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:mypcaddre";      // 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.txtzuma.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 complete file names in a given directory that matches the user-defined search and additionally explores folders.

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:mypcaddre";      // 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.txtzuma.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:mypcaddre";            // 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.txtzuma.txt

Conclusion

所以,这篇文章就到这里了。在这篇文章中,我们学习了如何编写一个C#程序来获取目录中的所有文件。我们讨论了不同的方法来实现这个目标。我们还了解了这些方法的算法,并学习了它们的代码。希望这篇文章能够增加你对C#的知识。

以上就是C# 程序获取目录中存在的所有文件的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1435056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 10:35:55
下一篇 2025年12月15日 03:10:16

相关推荐

  • 如何从 C# 中的 DateTime 对象中仅获取日期部分?

    有多种方法可以从 DateTime 对象中仅获取日期部分。 ToShortDateString() – 将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式. 返回一个字符串,其中包含当前的短日期字符串表示形式DateTime 对象。 ToLongDateString()…

    2025年12月17日
    000
  • C# 中的接口如何工作?

    接口定义了将由类或结构实现的契约。它可以包含方法、属性、事件和索引器。接口与类类似,只不过它不保存任何数据,仅指定它可以执行的行为(或更准确地说,实现它的类可以执行的行为)。 类可以实现一个行为或更多接口。要实现接口成员,类应具有与接口成员具有相同方法定义的公共成员,即相同的名称和签名。 例如,IC…

    2025年12月17日
    000
  • C# 中的序列化和反序列化

    序列化将对象转换为字节流,并将其转换为可以写入流的形式。这样做是为了将其保存到内存、文件或数据库中。 可以执行以下序列化操作: 二进制序列化 所有成员,甚至只读成员,都会被序列化。 XML序列化 它将对象的公共字段和属性序列化为符合特定XML模式定义语言文档的XML流。 让我们看一个例子。首先设置流…

    2025年12月17日
    000
  • 为什么 C# 中的单例类总是密封的?

    sealed 关键字意味着该类不能被继承。将构造函数声明为私有意味着无法创建该类的实例。 您可以拥有一个带有私有构造函数的基类,但仍然从该基类继承,定义一些公共构造函数,并有效地实例化该基类. 构造函数不是继承的(因此派生类不会仅仅因为基类具有所有私有构造函数),并且派生类始终首先调用基类构造函数。…

    2025年12月17日
    000
  • 如何在 C# 中重新抛出 InnerException 而不丢失堆栈跟踪?

    在c#中,throw是一个关键字,在程序执行过程中手动抛出异常很有用,我们可以根据需要使用try-catch块来处理这些抛出的异常。 通过在 catch 块中使用 throw 关键字,我们可以重新抛出在 catch 块中处理的异常。当我们想要将异常传递给调用者以按照他们想要的方式处理它时,重新抛出异…

    2025年12月17日
    000
  • 如何在 C# 中使用右移运算符?

    左操作数的值向右移动右移运算符中右操作数指定的位数。 让我们看一下 C# 中右移运算符的示例 – using System;namespace OperatorsAppl { class Program { static void Main(string[] args) { int a …

    2025年12月17日
    000
  • 如何在C#中打印多个空行?

    要显示多个空行,我们将使用 while 循环。 这里,我们使用 Console.WriteLine() 打印 10 个空行; while (a < 10) { Console.WriteLine(" "); a++;} 以下是显示多个空行的完整代码 – 示例 u…

    2025年12月17日
    000
  • C# ASP.NET Core 中间件与 HttpModule 有何不同?

    HttpModules 通过 web.config 或 global.asax 配置开发人员无法控制执行顺序。 因为模块的顺序主要基于应用程序生命周期事件。请求和响应的执行顺序保持相同。 HttpModules 可帮助您附加特定于应用程序事件的代码。HttpModules 绑定到 System.we…

    2025年12月17日
    000
  • C# 程序逐行读取文件内容

    简介 在这里,我们将学习编写一个 C# 程序来逐行读取文件内容。有多种方法可以做到这一点。我们将一一讨论。 文件处理是用 C# 完成的。大多数情况下,文件用于存储数据。通俗地说,文件处理或文件管理是各种过程,例如创建文件、读取文件、写入文件、附加文件等。文件的读取和写入是文件处理中最常见的两个操作。…

    2025年12月17日
    000
  • C# Asp.net Core中启动类的Configure()方法有什么用?

    configure 方法存在于 ASP.NET Core 应用程序的启动类中 Configure 方法是您可以配置应用程序请求管道的地方为您的应用程序使用内置提供的 IApplicationBuilder 实例IoC容器 Configure方法默认有这三个参数IApplicationBuilder,…

    2025年12月17日
    000
  • C# 中的 IList 接口有什么作用?

    IList 接口有一个非通用的对象集合,可以通过索引单独访问。 以下是 C# 中 IList 接口的属性 – 先生编号 属性名称和说明 th> 1Count 获取数字ICollection 中包含的元素数量。 2isFixedSize 获取一个值,指示 IList 是否具有固定大小…

    2025年12月17日
    000
  • C# 异常处理最佳实践

    C# 中异常处理的最佳实践基于记录异常。日志应该保存在日志库中以记录异常。 以下是在 C# 中处理异常时应遵循的最佳实践 – 使用 log4net、NLog 和其他用于相同目的的框架记录异常。将异常记录到文件中,并将日志发送到各种其他目标,例如数据库、电子邮件等。您应该在应用程序中记录每…

    2025年12月17日
    000
  • 如何在 C# 中使用反射来显示方法和属性?

    反射是在代码中描述类型、方法和字段的元数据的过程。命名空间 System.Reflection 使您能够获取有关已加载程序集、其中的元素(例如类、方法和值类型)的数据。 System.Reflection 的类有很多,但最常用的是 Assembly、AssemblyName、ConstructorI…

    2025年12月17日
    000
  • C# 中列表和字典有什么区别?

    字典是 C# 中键和值的集合。 Dictionary 包含在 System.Collection.Generics 命名空间中。 Dictionary 是一个泛型类型,如果您尝试查找不存在的键,则会返回错误。 List 集合是一个泛型类,可以存储任何数据类型来创建列表。 列表是一组项目 &#8211…

    2025年12月17日
    000
  • C#有哪些隐藏的特性?

    以下是 C# 隐藏的或鲜为人知的有用功能 – Lambda 表达式 C# 中的 lambda 表达式描述了一种模式。它在表达式上下文中具有标记=>。这称为 go to 运算符,并在声明 lambda 表达式时使用。 可空 C# 提供了一种特殊的数据类型,可空类型,您可以为其分配正常…

    2025年12月17日
    000
  • 在C#中,结构体是什么?

    在C#中,结构是一种值类型数据类型。它可以帮助您使单个变量持有不同数据类型的相关数据。使用struct关键字来创建结构。 C#结构具有以下特点: 结构可以有方法、字段、索引器、属性、运算符方法和事件。 结构可以有定义的构造函数,但没有析构函数。但是,您不能为结构定义默认构造函数。默认构造函数是自动定…

    2025年12月17日
    000
  • C# 程序在不使用 Reverse() 方法的情况下反转字符串

    在编程中,有很多情况我们需要反转字符串。最常见的方法之一是使用 Reverse() 方法。但是,在某些情况下我们无法使用此方法,而必须使用其他技术来反转字符串。在本文中,我们将探讨如何在不使用 Reverse() 方法的情况下在 C# 中反转字符串。 在深入研究代码之前,让我们首先了解什么是字符串。…

    2025年12月17日
    000
  • NonActionAttribute 在 ASP .Net MVC C# 中的意义是什么?

    当我们想要在控制器中使用公共方法但又不想将其视为操作方法时,可以使用 nonaction 属性。操作方法是控制器中的公共方法,可以使用 url 调用。因此,默认情况下,如果控制器中有任何公共方法,则可以使用 url 请求来调用它。要限制对控制器中公共方法的访问,可以使用 nonaction 属性。 …

    2025年12月17日
    000
  • C# 中的 Deque 类

    Deque 类使用双向链表来实现其元素集合。双向链表应该有两个节点,即前节点和后节点。这有助于在 Deque 的正面和背面添加元素。 使用 Deque 类,您可以从两侧添加和删除元素。这就是为什么 Deque 被称为双端队列。 Deque 类在 Queue 类中有以下方法 – Clear…

    2025年12月17日
    000
  • 在 C# 中使您的集合线程安全

    .NET Framework 4 引入了 System.Collections.Concurrent 命名空间。它有几个线程安全且可扩展的集合类。这些集合称为并发集合,因为它们可以同时被多个线程访问。 以下并发集合类型使用轻量级同步机制:SpinLock、SpinWait 等。这些是 .NET Fr…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信