在文件夹中查找具有特定扩展名的文件

发布于 2024-09-07 12:36:28 字数 136 浏览 2 评论 0原文

给定一个文件夹路径(例如C:\RandomFolder),如何在其中找到包含特定扩展名(例如txt)的文件?我假设我必须在目录中搜索 *.txt,但我不确定首先应该如何开始此搜索。

Given a folder path (like C:\Random Folder), how can I find a file in it that holds a certain extension, like txt? I assume I'll have to do a search for *.txt in the directory, but I'm not sure how I'm supposed to start this search in the first place.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

海螺姑娘 2024-09-14 12:36:28

查看System.IO.Directory 类和静态方法GetFiles。它具有接受路径和搜索模式的重载。例子:

 string[] files = System.IO.Directory.GetFiles(path, "*.txt");

Look at the System.IO.Directory class and the static method GetFiles. It has an overload that accepts a path and a search pattern. Example:

 string[] files = System.IO.Directory.GetFiles(path, "*.txt");
黎歌 2024-09-14 12:36:28

您可以使用 Directory

 Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories)

You could use the Directory class

 Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories)
南渊 2024-09-14 12:36:28

实际上,这很容易。您可以使用 System.IO.Directory 类与 System.IO 结合使用.路径。类似于(使用 LINQ 使其变得更加容易):

var allFilenames = Directory.EnumerateFiles(path).Select(p => Path.GetFileName(p));

// Get all filenames that have a .txt extension, excluding the extension
var candidates = allFilenames.Where(fn => Path.GetExtension(fn) == ".txt")
                             .Select(fn => Path.GetFileNameWithoutExtension(fn));

当然,这种技术也有很多变体。如果您的过滤器更简单,其他一些答案也会更简单。这个的优点是延迟枚举(如果这很重要的话)和更灵活的过滤,但代价是更多的代码。

It's quite easy, actually. You can use the System.IO.Directory class in conjunction with System.IO.Path. Something like (using LINQ makes it even easier):

var allFilenames = Directory.EnumerateFiles(path).Select(p => Path.GetFileName(p));

// Get all filenames that have a .txt extension, excluding the extension
var candidates = allFilenames.Where(fn => Path.GetExtension(fn) == ".txt")
                             .Select(fn => Path.GetFileNameWithoutExtension(fn));

There are many variations on this technique too, of course. Some of the other answers are simpler if your filter is simpler. This one has the advantage of the delayed enumeration (if that matters) and more flexible filtering at the expense of more code.

恰似旧人归 2024-09-14 12:36:28

下面的方法仅返回具有特定扩展名的文件(例如:带有 .txt 但不是 .txt1 的文件)

public static IEnumerable<string> GetFilesByExtension(string directoryPath, string extension, SearchOption searchOption)
    {
        return
            Directory.EnumerateFiles(directoryPath, "*" + extension, searchOption)
                .Where(x => string.Equals(Path.GetExtension(x), extension, StringComparison.InvariantCultureIgnoreCase));
    }

The method below returns only the files with certain extension (eg: file with .txt but not .txt1)

public static IEnumerable<string> GetFilesByExtension(string directoryPath, string extension, SearchOption searchOption)
    {
        return
            Directory.EnumerateFiles(directoryPath, "*" + extension, searchOption)
                .Where(x => string.Equals(Path.GetExtension(x), extension, StringComparison.InvariantCultureIgnoreCase));
    }
池予 2024-09-14 12:36:28

根据我的理解,这可以通过两种方式完成:

1)您可以使用 Directory 类和 Getfiles 方法并遍历所有文件以检查我们所需的扩展名。

Directory.GetFiles("your_folder_path)[i].Contains("*.txt")

2) 您可以将 Path 类与 GetExtension 方法一起使用,该方法将文件路径作为参数并验证扩展名。要获取文件路径,只需有一个循环条件将获取单个文件并返回可用于验证的文件路径。

Path.GetExtension(your_file_path).Equals(".json")

注意:两个逻辑都必须位于循环条件内。

As per my understanding, this can be done in two ways :

1) You can use Directory Class with Getfiles method and traverse across all files to check our required extension.

Directory.GetFiles("your_folder_path)[i].Contains("*.txt")

2) You can use Path Class with GetExtension Method which takes file path as a parameter and verifies the extension.To get the file path, just have a looping condition that will fetch a single file and return the filepath that can be used for verification.

Path.GetExtension(your_file_path).Equals(".json")

Note : Both the logic has to be inside a looping condition.

薄暮涼年 2024-09-14 12:36:28

使用此代码读取具有所有类型扩展文件的文件。

string[] sDirectoryInfo = Directory.GetFiles(SourcePath, "*.*");

Use this code for read file with all type of extension file.

string[] sDirectoryInfo = Directory.GetFiles(SourcePath, "*.*");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文