搜索多个文件扩展名?

编程入门 行业动态 更新时间:2024-10-26 10:28:47
本文介绍了搜索多个文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

通过这个网站,我在尝试扫描文件扩展名(例如* .txt)时发现了我最近的问题,但我无法弄清楚如何扫描多种文件类型,例如.txt和.exe 同时? 一次用于单个文件扩展名的代码是:

dim FExtension as string = * .txt 对于 每个 file1 在 IO.Directory.GetFiles(dir,FExtension)

如何添加?

dim FExtension as string = * .txt和' *。exe和* .sys

等等? 提前感谢您的帮助! :)

解决方案

你不能。 GetFiles方法只接受重载中的一个选项。 使用不同的扩展名运行两次,或者在没有过滤器的情况下运行它,然后在循环中过滤结果。

您可以使用LinQ检索具有多个扩展名的所有文件,如下所示: -

var files = Directory.GetFiles( @ C:\ yourourPath, *。*,SearchOption.AllDirectories)。 其中(s = > s.EndsWith( .txt,StringComparison.OrdinalIgnoreCase)|| s.EndsWith( 。doc,StringComparison.OrdinalIgnoreCase));

请注意原始查询将返回yourPath中的所有文件,所以如果你传入说C:\你将开始遇到性能问题。

我会尝试通过这样的文件扩展名来解决这个问题。 尽管它会被警告如果您选择根开始位置,即使只有1个文件扩展名也需要一些时间来返回所有信息。

Dim FExtension1 作为 字符串 = * .txt Dim FExtension2 作为 字符串 = * .exe Dim FExtension3 作为 字符串 = * .sys 对于 每个 file1 在 IO.Directory.GetFiles(Dir,FExtension1) ' 添加到列表框或附加到文本框或Datagridview 下一步 对于 每个 file1 在 IO.Directory.GetFiles(Dir,FExtension2) ' 添加到列表框或附加到文本框或Datagridview 下一步 For 每个 file1 在 IO.Directory.GetFiles(Dir,FExtension3) ' 添加到列表框或附加到文本框或Datagridview 下一步

Through this site I have figured out my recent problem when trying to scan for file extensions such as "*.txt", but I cannot figure out how to scan for multiple file types such as ".txt" and ".exe" at the same time? The code that works for a single file extension at a time is:

dim FExtension as string = "*.txt" For Each file1 In IO.Directory.GetFiles(dir, FExtension)

How can I add?

dim FExtension as string = "*.txt" and '*.exe" and "*.sys"

and so forth? Thank you in advance for your help! :)

解决方案

You can't. GetFiles method only accepts one option in the overload. Either run it twice with a different extension, or, run it without the filter and afterward filter the result in a loop.

You could use LinQ to retrieve all files with multiple extensions like this:-

var files = Directory.GetFiles(@"C:\yourPath", "*.*", SearchOption.AllDirectories). Where(s => s.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) || s.EndsWith(".doc", StringComparison.OrdinalIgnoreCase));

Just be aware that the original query will return all files in yourPath, so if you pass in say C:\ you will start getting performance issues.

I would try to recurse thru the file extensions something like this. Be Warned though it will take some time to return all of the information even for just 1 file extension if you choose a root start location.

Dim FExtension1 As String = "*.txt" Dim FExtension2 As String = "*.exe" Dim FExtension3 As String = "*.sys" For Each file1 In IO.Directory.GetFiles(Dir, FExtension1) ' add to list box or append to text box or a Datagridview Next For Each file1 In IO.Directory.GetFiles(Dir, FExtension2) ' add to list box or append to text box or a Datagridview Next For Each file1 In IO.Directory.GetFiles(Dir, FExtension3) ' add to list box or append to text box or a Datagridview Next

更多推荐

搜索多个文件扩展名?

本文发布于:2023-11-02 17:54:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553035.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文件扩展名

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!