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 − 首先,我们声明一个字符串来存储目录的地址。
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#的知识。
以上是C# 程序获取目录中存在的所有文件的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

在 C 语言中,char 类型在字符串中用于:1. 存储单个字符;2. 使用数组表示字符串并以 null 终止符结束;3. 通过字符串操作函数进行操作;4. 从键盘读取或输出字符串。

C 语言中符号的使用方法涵盖算术、赋值、条件、逻辑、位运算符等。算术运算符用于基本数学运算,赋值运算符用于赋值和加减乘除赋值,条件运算符用于根据条件执行不同操作,逻辑运算符用于逻辑操作,位运算符用于位级操作,特殊常量用于表示空指针、文件结束标记和非数字值。

C语言中通过转义序列处理特殊字符,如:\n表示换行符。\t表示制表符。使用转义序列或字符常量表示特殊字符,如char c = '\n'。注意,反斜杠需要转义两次。不同平台和编译器可能有不同的转义序列,请查阅文档。

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。

在 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 的字符范围更大,并使用专门的函数进行算术运算。

在 C 语言中,char 类型转换可以通过:强制类型转换:使用强制类型转换符将一种类型的数据直接转换为另一种类型。自动类型转换:当一种类型的数据可以容纳另一种类型的值时,编译器自动进行转换。

char 数组在 C 语言中存储字符序列,声明为 char array_name[size]。访问元素通过下标运算符,元素以空终止符 '\0' 结尾,用于表示字符串终点。C 语言提供多种字符串操作函数,如 strlen()、strcpy()、strcat() 和 strcmp()。

char 和 unsigned char 是存储字符数据的两种数据类型,主要区别在于处理负数和正数的方式:值范围:char 有符号 (-128 到 127),unsigned char 无符号 (0 到 255)。负数处理:char 可以存储负数,unsigned char 不能。位模式:char 最高位表示符号,unsigned char 无符号位。算术运算:char 和 unsigned char 作为有符号和无符号类型,其算术运算方式不同。兼容性:char 和 unsigned char
