简体   繁体   English

检查文件是否有多个扩展名?

[英]Check file against multiple extensions?

I'm wondering if it is possible to check if a file matches a list of set extention. 我想知道是否可以检查文件是否与设置扩展名列表匹配。 at the moment i have a working code that only checks a file to see if it has a .txt .jpg or .mp3 目前,我有一个工作代码,它仅检查文件以查看其是否具有.txt .jpg或.mp3

Example

If (Path.GetExtension(f).ToLower() = ".txt") Then
                ' It's a .txt file
                '         MsgBox("I am a Txt Document")
                b.Image = My.Resources.Text_Img

            ElseIf (Path.GetExtension(f).ToLower() = ".jpg") Then
                b.Image = My.Resources.Img_Img
            Else
                b.Image = My.Resources.Folder_Img
            End If

This code simply set the image for the Shortcut. 这段代码只是为快捷方式设置图像。 I want to be able to check if a file matches more than a txt but a .doc .log then all video types mp4 avi mpg. 我希望能够检查文件是否比txt但.doc .log匹配更多,然后所有视频类型mp4 avi mpg。 then all Audio types mp3 ogg flacc acc 然后所有音频类型mp3 ogg flacc acc

Tried, But Failed 尝试过但失败了

ElseIf (Path.GetExtension(f).ToLower() = ".jpg"  & ".png" & ".jpeg" & ".tiff") Then
    b.Image = My.Resources.Img_Img
End If

is this possible don't want to add all 100+ extension to each have their own If Statements. 是否有可能不想将所有100+扩展名添加到每个都有自己的If语句中。 Thank you in advanced. 在此先感谢您。 -Dan -担

If you want to avoid the dictionary route (for no real good reason, other than this being an alternative method), you can use an in-line array to check for each group of extensions. 如果要避免字典路由(没有真正的理由,除了这是一种替代方法),可以使用内联数组检查每组扩展。

Dim extension = Path.GetExtension(f).ToLower

If {".txt"}.Contains(extension) Then
    b.Image = My.Resources.Text_Img
ElseIf {".jpg", ".png", ".tif"}.Contains(extension) Then
    b.Image = My.Resources.Img_Img
Else
    b.Image = My.Resources.Folder_Img
End If

I'm sure there are perfectly valid and aesthetically pleasing methods using RegEx and all sorts of LINQ or other weird ways of doing it! 我敢肯定,使用RegEx和各种LINQ或其他怪异的方法可以使用完全有效且美观的方法!

Rather than an If you could look up the code from a dictionary of values. 而不是If可以从值字典中查找代码。 Declare the dictionary as: 将字典声明为:

Dim dictionary As New Dictionary(Of String, TheResourceType)
dictionary.Add(".txt", My.Resources.Text_Img)
dictionary.Add(".jpg", My.Resources.Img_Img)
dictionary.Add(".png", My.Resources.Img_Img)
dictionary.Add(".jpeg", My.Resources.Img_Img)
dictionary.Add(".tiff", My.Resources.Img_Img)

The question is not clear about the type of b.Image or the My.Resources... values, so change TheResourceType as needed. 问题尚不清楚b.ImageMy.Resources...值的类型,因此TheResourceType根据需要更改TheResourceType

Access the values with code like: 使用以下代码访问值:

Dim ext As Path.GetExtension(f).ToLower()
If dictionary.ContainsKey(ext) Then
    b.Image = dictionary.Item(ext)
Else
    b.Image = My.Resources.Folder_Img
End If

Do something like that: 做这样的事情:

Dim extension As String = Path.GetExtension(f).ToLower()

If extension.Equals("txt") Then
    b.Image = My.Resources.Text_Img
ElseIf extension.Equals("jpg") OrElse extension.Equals("png") OrElse extension.Equals("tif") Then
    b.Image = My.Resources.Img_Img
Else
    b.Image = My.Resources.Folder_Img
End If

In above code Path.GetExtension() method is called only once, instead of every time the extension is checked. 在上面的代码中,仅一次调用Path.GetExtension()方法,而不是每次检查扩展名。 Second, it uses string build-in Equals() method to check extensions. 其次,它使用字符串内置Equals()方法检查扩展名。 Third, syntax of the check against multiple extensions is fixed - look at the second condition regarding the image files - it is slightly different than yours. 第三,针对多个扩展名进行检查的语法是固定的-查看有关图像文件的第二个条件-它与您的稍有不同。

You may have already considered this, but another option (if this is a common need) would be to create a database table of common extensions and then write a function to query that table, passing in Path.GetExtension as a parameter. 您可能已经考虑过这一点,但是另一个选择(如果这是普遍需要)将是创建具有公共扩展名的数据库表,然后编写一个函数来查询该表,并将Path.GetExtension作为参数传递。

An exhaustive list of extensions for such database table can be found here: http://www.freeformatter.com/mime-types-list.html 可以在以下位置找到此类数据库表的详尽扩展列表: http : //www.freeformatter.com/mime-types-list.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM