用编程方式给IIS增加MIME类型

      前段时间,在完成FLV视频播放,遭遇一个“在服务器上无法播放FLV视频”问题。奇怪的是在我本机上试验是可以的,当放到服务器上就不行。(程序运行环境:windows 2003 +IIs 6.0

刚开始以为可能是FLV视频文件太大,导致IIS不能下载FLV视频,不能播放。于是修改了IIS附件下载限制,可是到最终在服务器上还是不能播放FLV视频。后来在网络上看到IIS是不支持播放Flash视频,这下可槽糕了,但怎么想也想不通啊,很多视频网站基本上都采用FLASH播放,他们是怎么做到的呢。通过网上资料,最终寻找到ADOBD公司给出“Windows 2003 Server does not stream FLV videos”解决方案:(原文如下:http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_19439&sliceId=1

Please be aware that these steps do not resolve any issue with Flash, but are a configuration step for Microsoft Windows 2003 and Microsoft IIS Server 6.0. Any difficulties in executing these instructions or any errors that may arise from modifying your system settings should be addressed to Microsoft. For more details, please refer to your IIS documentation.

  1. On the Windows 2003 server, open the Internet Information Services Manager.
  2. Expand the Local Computer Server.
  3. Right-click the local computer server and select Properties.
  4. Select the MIME Types tab.
  5. Click New and enter the following information:
    • Associated Extension box: .FLV
    • MIME Type box:flv-application/octet-stream
  6. Click OK.
  7. Restart the World Wide Web Publishing service.

       设置好后,播放器果然可以播放了。到目前为止,可能有人会问,好像跟该文章主题“用编程方式给IIS增加MIME类型”,没有多大关系吧。好,现在就开始进入正题,既然可以手动设置IISMIME类型,那能不能通过程序方式自动给IIS添加MIME类型。那答案是肯定的。只要能获取到IIS控制台的MIME属性就OK了。

 IIS的所有操作都可以由System.DirectoryServices来完成。同样地,MIME也应该是可以的,在MSDN中找到一篇关于“Setting MIME Type Properties Using System.DirectoryServices”文章,原文如下http://msdn2.microsoft.com/en-us/library/ms525901.aspx关键的地方就是引用了Active DS IIS Namespace Provider。这样就得到了IISOLE.IISMimeType类。

具体代码如下:
   

using System;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace SettingMIMEType
{
    
class Program
    
{
        
static void Main(string[] args)
        
{

            
string serverName = GetIISPath(args[0]);
            
if (string.IsNullOrEmpty(serverName))
            
{
                Console.WriteLine(
"IIS ServerName is Null");
            }

            
else
            
{
                SetMimeTypeProperty(serverName, args[
1], args[2]);
            }

         
            Console.ReadLine();
        }


        
static string GetIISPath(string siteName) 
        
{
            
string iisPath="IIS://localhost/W3SVC/{0}/Root";
            
string result = string.Empty;
            
int siteID = GetWebSiteID(siteName);
            
if (siteID > 0)
            
{
                result 
= string.Format(iisPath, siteID);
            }

            
else 
            
{
                Console.WriteLine(
"IIS://<servername>/ Is Failed");
            }

            
return result;
        }


        
static int GetWebSiteID(string siteName)
        
{
            DirectoryEntry root 
= new DirectoryEntry("IIS://localhost/W3SVC");
            
foreach (DirectoryEntry dir in root.Children)
            
{
                
if (dir.SchemaClassName == "IIsWebServer")
                
{
                    
string site = dir.Properties["ServerComment"].Value.ToString();
                    
if (site.Equals(siteName, StringComparison.OrdinalIgnoreCase))
                    
{
                        
return int.Parse(dir.Name);
                    }

                }

            }

            
return -1;
        }


        
static void SetMimeTypeProperty(string metabasePath, string newExtension, string newMimeType)
        
{
            Console.WriteLine(
"\nAdding {1}->{2} to the MimeMap property at {0}:",
                metabasePath, newExtension, newMimeType);

            
try
            
{
                DirectoryEntry path 
= new DirectoryEntry(metabasePath);
                PropertyValueCollection propValues 
= path.Properties["MimeMap"];
                Console.WriteLine(
" Old value of MimeMap has {0} elements", propValues.Count);
                
object exists = null;

                
foreach (object value in propValues)
                
{         
                    IISOle.IISMimeType mimetypeObj 
= (IISOle.IISMimeType)value;         
                    Console.WriteLine(
"  {0}->{1}", mimetypeObj.Extension, mimetypeObj.MimeType);
                    
if (newExtension == mimetypeObj.Extension)
                        exists 
= value;
                }


                
if (null != exists)
                
{
                    propValues.Remove(exists);
                    Console.WriteLine(
" Found an entry for {0}; removing it before adding the new one.",
                        newExtension);
                }


                IISOle.MimeMapClass newObj 
= new IISOle.MimeMapClass();
                newObj.Extension 
= newExtension;
                newObj.MimeType 
= newMimeType;
                propValues.Add(newObj);
                path.CommitChanges();
                Console.WriteLine(
" Done.");
            }

            
catch (Exception ex)
            
{
                
if ("HRESULT 0x80005006" == ex.Message)
                    Console.WriteLine(
" Property MimeMap does not exist at {0}", metabasePath);
                
else
                    Console.WriteLine(
"Failed in SetMimeTypeProperty with the following exception: \n{0}",
                        ex.Message);
            }

        }

    }

}


以上代码在WINDWOS2003 + Visual Studio 2008 + IIS 6.0环境中,测试通过。

posted @ 2008-02-12 16:55  seeker  阅读(1199)  评论(0编辑  收藏  举报