これは別の同様の実装ですが、COM 参照を追加する必要はありません。代わりに、リフレクションを介してプロパティを取得し、簡単に検索できるように NameValueCollection に格納します。
using System.Collections.Specialized; //NameValueCollection
using System.DirectoryServices; //DirectoryEntry, PropertyValueCollection
using System.Reflection; //BindingFlags
NameValueCollection map = new NameValueCollection();
using (DirectoryEntry entry = new DirectoryEntry("IIS://localhost/MimeMap"))
{
PropertyValueCollection properties = entry.Properties["MimeMap"];
Type t = properties[0].GetType();
foreach (object property in properties)
{
BindingFlags f = BindingFlags.GetProperty;
string ext = t.InvokeMember("Extension", f, null, property, null) as String;
string mime = t.InvokeMember("MimeType", f, null, property, null) as String;
map.Add(ext, mime);
}
}
そのルックアップ テーブルを非常に簡単にキャッシュして、後で参照することができます。
Response.ContentType = map[ext] ?? "binary/octet-stream";