LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

[点晴永久免费OA]C#动态添加IIS个各个版本的MIME类型

admin
2022年4月19日 16:35 本文热度 1642

主要是介绍IIS6和IIS7。IIS6主要是运用System.DirectoryServices.dll,IIS7用到Microsoft.Web.Administration.dll,该DLL在IIS7目录中存在,拷贝即可。


代码如下:

/// <summary>

/// 取得IIS版本

/// </summary>

/// <returns></returns>

private int GetIISVersion()

{

DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");

string strVersion = getEntity.Properties["MajorIISVersionNumber"].Value.ToString();

return Convert.ToInt32(strVersion);

}


主体实现方法:

private void Main() {

//判断IIS版本

int iisVersion = this.GetIISVersion();

//IIS7

if (iisVersion > 6)

{

try

{

ServerManager server = new ServerManager();

Configuration confg = server.GetWebConfiguration(webSiteName); //webSiteName站点名称

ConfigurationSection section;

section = confg.GetSection("system.webServer/staticContent"); //取得MimeMap所有节点(路径为:%windir%\Windows\System32\inetsrv\config\applicationHost.config)

ConfigurationElement filesElement = section.GetCollection();

ConfigurationElementCollection filesCollection = filesElement.GetCollection();

ConfigurationElement newElement = filesCollection.createElement(); //新建MimeMap节点

newElement.Attributes["fileExtension"].Value = this.txtName.Text;

newElement.Attributes["mimeType"].Value = this.txtMimeType.Text;

if (filesCollection.Contains(newElement))

{

MessageBox.Show("您添加的类型已存在,请重新添加!");

return;

}

filesCollection.Add(newElement);

server.CommitChanges(); //更改目录

MessageBox.Show("添加成功!");

this.Close();

}

catch (Exception ex)

{

MessageBox.Show("eror:" + ex.Message.ToString());

}

}

else //IIS6

{

try

{

int id = this.GetselectIdByWebSiteName(webSiteName); //取得站点的标识符ID

DirectoryEntry path = new DirectoryEntry(string.Format("IIS://localhost/w3svc/{0}/root", id));

PropertyValueCollection propValues = path.Properties["MimeMap"];

string newExtension = this.txtName.Text; //扩展名

string newMimeType = this.txtMimeType.Text; //MIME类型

object exists = null;

foreach (object value in propValues)

{

IISOle.IISMimeType mimetypeObj = (IISOle.IISMimeType)value;

if (newExtension == mimetypeObj.Extension){exists = value;}

}

if (null != exists)

{

//propValues.Remove(exists);

MessageBox.Show("您添加的类型已存在,请重新添加!");

return;

}

IISOle.MimeMapClass _NewMime = new IISOle.MimeMapClass(); //新建MIME类型

_NewMime.Extension = newExtension;

_NewMime.MimeType = newMimeType;

propValues.Add(_NewMime); //添加MIME类型

path.CommitChanges(); //更改目录

MessageBox.Show("添加成功!");

this.Close();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message.ToString());

}

}

}


该文章在 2022/4/19 16:35:25 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved