当前位置: 首页 > news >正文

用编程方式给iis里增加mime类型

最近在搞一个安装程序,其中有一步,是要给iis增加一个新的mime类型.log,好让用户能下载iis的日志。



iis的所有的操作都可以用System.DirectoryServices里的功能完成。mime类型一定也可以。果然,经过一翻寻找,在msdn中找到。原文如下: http://msdn2.microsoft.com/en-us/library/ms525901.aspx

这里面关键一步是引用 Active DS IIS Namespace Provider,这样你就能使用 IISOle这个命名空间,和 IISMimeType 这个类。


实际代码并不复杂,全部在下面,比较简单,就不多解释。


 
ContractedBlock.gif ExpandedBlockStart.gif MIME
None.gifusing System;
None.gif
using System.IO;
None.gif
using System.DirectoryServices;
None.gif
using System.Reflection;
None.gif
using System.Runtime.InteropServices;
None.gif
using System.Collections;
None.gif
None.gif
namespace System_DirectoryServices_DirectoryEntry_ConfigIIS
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SetMimeTypeProperty(
"IIS://localhost/W3SVC/1/Root"".hlp""application/winhlp");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static void SetMimeTypeProperty(string metabasePath, string newExtension, string newMimeType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
//  metabasePath is of the form "IIS://<servername>/<path>"
InBlock.gif      
//    for example "IIS://localhost/W3SVC/1/Root" 
InBlock.gif      
//  newExtension is of the form ".<extension>", for example, ".hlp"
InBlock.gif      
//  newMimeType is of the form "<application>", for example, "application/winhlp"
InBlock.gif
      Console.WriteLine("\nAdding {1}->{2} to the MimeMap property at {0}:", metabasePath, newExtension, newMimeType);
InBlock.gif
InBlock.gif      
try
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          DirectoryEntry path 
= new DirectoryEntry(metabasePath);
InBlock.gif          PropertyValueCollection propValues 
= path.Properties["MimeMap"];
InBlock.gif          Console.WriteLine(
" Old value of MimeMap has {0} elements", propValues.Count);
InBlock.gif
InBlock.gif          
object exists = null;
InBlock.gif          
foreach (object value in propValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif              
// IISOle requires a reference to the Active DS IIS Namespace Provider in Visual Studio .NET
InBlock.gif
              IISOle.IISMimeType mimetypeObj = (IISOle.IISMimeType)value;
InBlock.gif              Console.WriteLine(
"  {0}->{1}", mimetypeObj.Extension, mimetypeObj.MimeType);
InBlock.gif              
if (newExtension == mimetypeObj.Extension)
InBlock.gif                  exists 
= value;
ExpandedSubBlockEnd.gif          }

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

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

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

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/jpwar/archive/2007/03/15/675956.html

相关文章:

  • 自定义制作 自动定时更换图片的桌面背景 html页面
  • 按比例缩放图片
  • Varchar与char的区别
  • 各种Excel VBA的命令
  • 网络游戏 聊天 下载 视频 股票等端口
  • [新闻]微软Silverlight着眼网络设计未来
  • 小小设置 让你的WinRAR用起来更顺手
  • C++ 类访问控制public/private/protected探讨
  • Just Thinking...
  • 什么是Page Rank及其由来 – Google 的民主表决式网页排名技术
  • 利用BCP导入数据到SQL中
  • Windows Mobile 5.0模拟器用ActiveSync来使用PC机的网络
  • More than meets the eye
  • CEO十五条法则 (是基于对CEO更加的关怀)
  • java和c#企业开发
  • CODING 缺陷管理功能正式开始公测
  • isset在php5.6-和php7.0+的一些差异
  • js ES6 求数组的交集,并集,还有差集
  • php中curl和soap方式请求服务超时问题
  • Rancher-k8s加速安装文档
  • Redux系列x:源码分析
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • Vue学习第二天
  • 测试如何在敏捷团队中工作?
  • 从零开始的无人驾驶 1
  • 电商搜索引擎的架构设计和性能优化
  • 深入 Nginx 之配置篇
  • 思维导图—你不知道的JavaScript中卷
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 微信公众号开发小记——5.python微信红包
  • 一些关于Rust在2019年的思考
  • 终端用户监控:真实用户监控还是模拟监控?
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • Semaphore
  • 积累各种好的链接
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • # .NET Framework中使用命名管道进行进程间通信
  • (笔试题)合法字符串
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (万字长文)Spring的核心知识尽揽其中
  • (一)Dubbo快速入门、介绍、使用
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • (转)Mysql的优化设置
  • ******IT公司面试题汇总+优秀技术博客汇总
  • ***利用Ms05002溢出找“肉鸡
  • ***通过什么方式***网吧
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .net 7 上传文件踩坑
  • .Net 高效开发之不可错过的实用工具
  • .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
  • .Net 知识杂记
  • .net连接oracle数据库