0

0

C# 程序获取目录中存在的所有文件

PHPz

PHPz

发布时间:2023-08-29 21:37:06

|

1862人浏览过

|

来源于tutorialspoint

转载

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.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  首先,我们声明一个字符串来存储目录的地址。

DM6在线读报系统
DM6在线读报系统

DM6在线读报系统ASPX 免费版2.0。如果您是一个DM广告公司的网站管理员,正在寻求一套程序或源码可以让公司网站具有一套配合网站整体架构的电子杂志频道,那您现在可找对了。请仔细阅读以下关于DM6在线读报系统的说明。 这是一个网站用户可以直接在线阅读报纸且无需插件(连Flash都不用)、无需下载、无需安装的在线读报系统(服务器端模块),通过将此系统放到网站文件目录中即可轻松生成网站的在线读报频道

下载

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 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:\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#的知识。

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
js正则表达式
js正则表达式

php中文网为大家提供各种js正则表达式语法大全以及各种js正则表达式使用的方法,还有更多js正则表达式的相关文章、相关下载、相关课程,供大家免费下载体验。

510

2023.06.20

正则表达式不包含
正则表达式不包含

正则表达式,又称规则表达式,,是一种文本模式,包括普通字符和特殊字符,是计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串,通常被用来检索、替换那些符合某个模式的文本。php中文网给大家带来了有关正则表达式的相关教程以及文章,希望对大家能有所帮助。

251

2023.07.05

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

745

2023.07.05

java正则表达式匹配字符串
java正则表达式匹配字符串

在Java中,我们可以使用正则表达式来匹配字符串。本专题为大家带来java正则表达式匹配字符串的相关内容,帮助大家解决问题。

213

2023.08.11

正则表达式空格
正则表达式空格

正则表达式空格可以用“s”来表示,它是一个特殊的元字符,用于匹配任意空白字符,包括空格、制表符、换行符等。本专题为大家提供正则表达式相关的文章、下载、课程内容,供大家免费下载体验。

351

2023.08.31

Python爬虫获取数据的方法
Python爬虫获取数据的方法

Python爬虫可以通过请求库发送HTTP请求、解析库解析HTML、正则表达式提取数据,或使用数据抓取框架来获取数据。更多关于Python爬虫相关知识。详情阅读本专题下面的文章。php中文网欢迎大家前来学习。

293

2023.11.13

正则表达式空格如何表示
正则表达式空格如何表示

正则表达式空格可以用“s”来表示,它是一个特殊的元字符,用于匹配任意空白字符,包括空格、制表符、换行符等。想了解更多正则表达式空格怎么表示的内容,可以访问下面的文章。

234

2023.11.17

正则表达式中如何匹配数字
正则表达式中如何匹配数字

正则表达式中可以通过匹配单个数字、匹配多个数字、匹配固定长度的数字、匹配整数和小数、匹配负数和匹配科学计数法表示的数字的方法匹配数字。更多关于正则表达式的相关知识详情请看本专题下面的文章。php中文网欢迎大家前来学习。

528

2023.12.06

c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

45

2026.01.23

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号