-2

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?

jww
  • 97,681
  • 90
  • 411
  • 885
Karolis
  • 89
  • 10

1 Answers1

0

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.

the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • your answer is similar to [this](http://stackoverflow.com/a/4465905/2425747) but yours don't have auto finding function. And i am using `System.Net.HttpListener`. – Karolis Apr 07 '14 at 16:09
  • 1
    Ah! I tought you asked for an alternative to case. Not an alternative to get the mime type... The list could be stored once using global.asax There's this function http://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping(v=vs.110).aspx – the_lotus Apr 07 '14 at 17:07