Question

I am writing advanced html server for my own purpose. Now i reached problem when specifying file type. Currently i determinate file type by extension

My function code:

Select Case extension.ToLower()
   Case ".avi"
      Return "video/x-msvideo"
   Case ".css"
      Return "text/css"
   Case ".doc"
      Return "application/msword"
   Case ".htm", ".html"
      Return "text/html"
      ...
   Case Else
      Return "application/octet-stream"
End Select

I use it like that:

Dim context as Net.HttpListenerContext
Dim file as String
context.Response.ContentType = getFileType(Path.GetExtension(file))

I accept answers also in C#

Edit: My question is how i can simply get file type without registry and case?

Était-ce utile?

La solution

You could always use a dictionary and put the information in a setting file.

    Dim mimeTypeList As New Dictionary(Of String, String)

    mimeTypeList.Add(".avi", "video/x-msvideo")
    mimeTypeList.Add(".css", "text/css")
    mimeTypeList.Add(".avi", "application/msword")

    ' ...

    If mimeTypeList.Contains(extension.ToLower()) Then
        Return mimeTypeList(extension.ToLower())
    End If

    Return "application/octet-stream"

But I'm pretty sure IIS already has all of this built-in.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top