搜索多个文件扩展名? [英] Search For Multiple File Extensions?

查看:111
本文介绍了搜索多个文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



一次用于单个文件扩展名的代码是:

  dim  FExtension  as   string  = < span class =code-string>  * .txt 
对于 每个 file1 IO.Directory.GetFiles(dir,FExtension)





如何添加?

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



等等?



提前感谢您的帮助! :)

解决方案

你不能。



GetFiles方法只接受重载中的一个选项。



使用不同的扩展名运行两次,或者在没有过滤器的情况下运行它,然后在循环中过滤结果。


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



  var  files = Directory.GetFiles( @  C:\ yourourPath,< span class =code-string>  *。*,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)
< span class =code-comment>' 添加到列表框或附加到文本框或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


这篇关于搜索多个文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆