基于扩展名vb.net获取文件类型
我需要根据上传图像的文件类型为每个组显示默认图像。就是 我有文件组作为 {.jpeg、.jpg、.png、.bmp、.gif、.tif}的图片 {.doc、.docx、.txt、.htm、.html、.xml、.xaml、.css}的文档 excel用于{.xls、.xlsx、.xlt、.xla} pdf格式 {.7z、.APK、.BAT、.rar、.dll、.jar、.zip}的zip 如果输入扩展名是.xls,那么我需要输出为excel 我试过选择案例 <>但是代码非常冗长,我怎么能简短呢? 你可以使用这种操作的概念:考虑下面的代码基于扩展名vb.net获取文件类型,vb.net,Vb.net,我需要根据上传图像的文件类型为每个组显示默认图像。就是 我有文件组作为 {.jpeg、.jpg、.png、.bmp、.gif、.tif}的图片 {.doc、.docx、.txt、.htm、.html、.xml、.xaml、.css}的文档 excel用于{.xls、.xlsx、.xlt、.xla} pdf格式 {.7z、.APK、.BAT、.rar、.dll、.jar、.zip}的zip 如果输入扩展名是.xls,那么我需要输出为excel 我试过选择案例 但是代码非常冗长,我怎么能简短呢? 你
Private Sub getfileType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getfileType.Click
Dim array As String()
Dim extensionType As String
Dim dics As New Dictionary(Of String, String()) From { _
{"picture", New String() {".jpeg", ".jpg", ".png", ".bmp", ".gif", ".tif"}}, _
{"document", New String() {".doc", ".docx", ".txt", ".htm", ".html", ".xml", ".xaml", ".css"}}, _
{"excel", New String() {".xls", ".xlsx", ".xlt", ".xla"}}, _
{"pdf", New String() {".pdf"}}, _
{"zip", New String() {".7z", ".APK", ".BAT", ".rar", ".dll", ".jar", ".zip"}}, _
{"ppt", New String() {".ppt", ".pos", ".pps"}}}
Dim pair As KeyValuePair(Of String, String())
For Each pair In dics
If attachmentType(pair.Key, pair.Value, ".APK") = True Then
extensionType = pair.Key
Exit For
End If
Next
MsgBox("File Type is :" & extensionType)
End Sub
Public Function attachmentType(ByVal input As String, ByVal array() As String, ByVal search As String) As Boolean
Dim temp As Integer = 0
For i As Integer = 0 To array.Length - 1
If array(i) = search Then temp = 1
Next
If temp = 1 Then
attachmentType = True
Else
attachmentType = False
End If
End Function
您可以使用这种操作的概念:考虑下面的代码
Private Sub getfileType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getfileType.Click
Dim array As String()
Dim extensionType As String
Dim dics As New Dictionary(Of String, String()) From { _
{"picture", New String() {".jpeg", ".jpg", ".png", ".bmp", ".gif", ".tif"}}, _
{"document", New String() {".doc", ".docx", ".txt", ".htm", ".html", ".xml", ".xaml", ".css"}}, _
{"excel", New String() {".xls", ".xlsx", ".xlt", ".xla"}}, _
{"pdf", New String() {".pdf"}}, _
{"zip", New String() {".7z", ".APK", ".BAT", ".rar", ".dll", ".jar", ".zip"}}, _
{"ppt", New String() {".ppt", ".pos", ".pps"}}}
Dim pair As KeyValuePair(Of String, String())
For Each pair In dics
If attachmentType(pair.Key, pair.Value, ".APK") = True Then
extensionType = pair.Key
Exit For
End If
Next
MsgBox("File Type is :" & extensionType)
End Sub
Public Function attachmentType(ByVal input As String, ByVal array() As String, ByVal search As String) As Boolean
Dim temp As Integer = 0
For i As Integer = 0 To array.Length - 1
If array(i) = search Then temp = 1
Next
If temp = 1 Then
attachmentType = True
Else
attachmentType = False
End If
End Function
可以为每行指定多个案例条件:
Select Case search
Case ".doc", ".docx", ".txt"
extensionType = "document"
Case ".jpeg", ".jpg", ".png", ".bmp"
extensionType = "picture"
End Select
或者您也可以使用dict,但将扩展名作为键:
Dim map As New Dictionary(Of String, String)
map.Add(".doc", "document")
map.Add(".docx", "document")
map.Add(".jpg", "picture")
map.Add(".png", "picture")
Console.WriteLine(map(".docx"))
可以为每行指定多个案例条件:
Select Case search
Case ".doc", ".docx", ".txt"
extensionType = "document"
Case ".jpeg", ".jpg", ".png", ".bmp"
extensionType = "picture"
End Select
或者您也可以使用dict,但将扩展名作为键:
Dim map As New Dictionary(Of String, String)
map.Add(".doc", "document")
map.Add(".docx", "document")
map.Add(".jpg", "picture")
map.Add(".png", "picture")
Console.WriteLine(map(".docx"))