在主机上创建虚拟目录,并修改默认站点的端口为8080:
Code
/***************************************************************************************************
* Filename: IISVirtualWebSite.cs
* Module: IIS 创建虚拟目录
* Copyright: 2008 穗联软件 版权所有
* Author: 周林郁
* Created Date: 2008-12-29
* Last Modified Data:
* Description: 需要引用COM 组件,Active DS IIS Namespace Provider
***************************************************************************************************/
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.DirectoryServices;
namespace MonitorService
{
public class IISVirtualWebSite
{
WriteLog wl = new WriteLog();
/// <summary>
/// 在主机上创建虚拟目录,并修改默认站点的端口为8080
/// </summary>
public string CreatVirWebSite(string _virtualDirName, string _physicalPath)
{
try
{
// 站点ID
int _WebSiteID = 1;
_WebSiteID = GetSiteID("localhost", "localhost");
if (_WebSiteID == 0)
{
_WebSiteID = 1;
}
if (_virtualDirName == "")
{
_virtualDirName = "Monitor";
}
String constIISWebSiteRoot = "IIS://localhost/W3SVC/" + _WebSiteID.ToString() + "/ROOT";
string virtualDirName = _virtualDirName; //虚拟目录名称:Monitor
string physicalPath = _physicalPath; //虚拟目录物理路径 C:\1
if (physicalPath == "")
{
physicalPath = @"D:\Program Files\Macromedia\Flash Media Server 2\applications\MonitorLocal\streams\_definst_\";
}
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
// 如果虚拟目录存在,则删除
foreach (System.DirectoryServices.DirectoryEntry v in root.Children)
{
if (v.Name == _virtualDirName)
{
// Delete the specified virtual directory if it already exists
try
{
root.Invoke("Delete", new string[] { v.SchemaClassName, _virtualDirName });
root.CommitChanges();
}
catch (Exception ex)
{
//sRet += ex.Message;
}
}
}
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, "IIsWebVirtualDir");
tbEntry.Properties["Path"][0] = physicalPath; //虚拟目录物理路径
tbEntry.Invoke("AppCreate", true);
tbEntry.Properties["AccessRead"][0] = true; //设置读取权限
tbEntry.Properties["ContentIndexed"][0] = true;
tbEntry.Properties["DefaultDoc"][0] = "index.aspx,Default.aspx"; //设置默认文档,多值情况下中间用逗号分割
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName; //虚拟目录名称:Monitor
tbEntry.Properties["AccessScript"][0] = true; //执行权限
tbEntry.Properties["AppIsolated"][0] = "1";
tbEntry.Properties["DontLog"][0] = true; // 是否记录日志
//tbEntry.Properties["AuthFlags"][0] = 0; // 设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证
tbEntry.Properties["AuthFlags"][0] = 1;
tbEntry.CommitChanges();
// 增加MIME 的类型:flv
SetMimeTypeProperty(constIISWebSiteRoot, ".flv", "flv");
// 修改IIS属性
SetSingleProperty("IIS://localhost/W3SVC/" + _WebSiteID.ToString(), "ServerBindings", ":8080:");
////得到现默认站点的IP 端口 描述
//string strServerBindings = root.Properties["ServerBindings"].Value.ToString();
////解出端口Port
//string[] strArr = strServerBindings.Split(':');
////重新赋值为8080
//root.Properties["ServerBindings"].Value = strArr[0] + ":8080:" + strArr[2];
root.CommitChanges();//确认更改
return "";
}
catch(Exception ex)
{
//wl.WriteLogMsg("[MonitorService]:创建虚拟目录失败: " + DateTime.Now.ToString() + "\r\n"+ ex.ToString() +"\r\n");
return ex.ToString();
}
}
#region 根据站点名称获取站点ID(标识)
/// <summary>
/// 根据站点名称获取站点ID(标识)
/// </summary>
/// <param name="webSiteName">站点名称</param>
/// <param name="serverIP">主机IP</param>
/// <returns>站点ID</returns>
private int GetSiteID(string webSiteName, string serverIP)
{
int SiteID = 0;
try
{
DirectoryEntry root = new DirectoryEntry(@"IIS://" + serverIP + "/W3SVC");
foreach (DirectoryEntry Child in root.Children)
{
PropertyValueCollection pvc = Child.Properties["ServerBindings"];
string WName = Child.Properties["ServerComment"][0].ToString();
if (webSiteName == WName)
{
SiteID = Convert.ToInt32(Child.Name);
return SiteID;
}
WName = "";
}
}
catch
{
SiteID = 0;
}
return SiteID;
}
#endregion
/// <summary>
/// 设置IIS的MIME类型,SetMimeTypeProperty("IIS://localhost/W3SVC/1/Root", ".hlp", "application/winhlp");
/// </summary>
/// <param name="metabasePath"></param>
/// <param name="newExtension"></param>
/// <param name="newMimeType"></param>
static void SetMimeTypeProperty(string metabasePath, string newExtension, string newMimeType)
{
// metabasePath is of the form "IIS://<servername>/<path>"
// for example "IIS://localhost/W3SVC/1/Root"
// newExtension is of the form ".<extension>", for example, ".hlp"
// newMimeType is of the form "<application>", for example, "application/winhlp"
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 requires a reference to the Active DS IIS Namespace Provider in Visual Studio .NET
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);
}
}
/// <summary>
/// 修改IIS属性
/// </summary>
/// <param name="metabasePath"></param>
/// <param name="propertyName"></param>
/// <param name="newValue"></param>
static void SetSingleProperty(string metabasePath, string propertyName, object newValue)
{
// metabasePath is of the form "IIS://<servername>/<path>"
// for example "IIS://localhost/W3SVC/1"
// propertyName is of the form "<propertyName>", for example "ServerBindings"
// value is of the form "<intStringOrBool>", for example, ":80:"
Console.WriteLine("\nSetting single property at {0}/{1} to {2} ({3}):",
metabasePath, propertyName, newValue, newValue.GetType().ToString());
try
{
DirectoryEntry path = new DirectoryEntry(metabasePath);
PropertyValueCollection propValues = path.Properties[propertyName];
string oldType = propValues.Value.GetType().ToString();
string newType = newValue.GetType().ToString();
Console.WriteLine(" Old value of {0} is {1} ({2})", propertyName, propValues.Value, oldType);
if (newType == oldType)
{
path.Properties[propertyName][0] = newValue;
path.CommitChanges();
Console.WriteLine("Done");
}
else
Console.WriteLine(" Failed in SetSingleProperty; type of new value does not match property");
}
catch (Exception ex)
{
if ("HRESULT 0x80005006" == ex.Message)
Console.WriteLine(" Property {0} does not exist at {1}", propertyName, metabasePath);
else
Console.WriteLine("Failed in SetSingleProperty with the following exception: \n{0}", ex.Message);
}
}
}
}
/***************************************************************************************************
* Filename: IISVirtualWebSite.cs
* Module: IIS 创建虚拟目录
* Copyright: 2008 穗联软件 版权所有
* Author: 周林郁
* Created Date: 2008-12-29
* Last Modified Data:
* Description: 需要引用COM 组件,Active DS IIS Namespace Provider
***************************************************************************************************/
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.DirectoryServices;
namespace MonitorService
{
public class IISVirtualWebSite
{
WriteLog wl = new WriteLog();
/// <summary>
/// 在主机上创建虚拟目录,并修改默认站点的端口为8080
/// </summary>
public string CreatVirWebSite(string _virtualDirName, string _physicalPath)
{
try
{
// 站点ID
int _WebSiteID = 1;
_WebSiteID = GetSiteID("localhost", "localhost");
if (_WebSiteID == 0)
{
_WebSiteID = 1;
}
if (_virtualDirName == "")
{
_virtualDirName = "Monitor";
}
String constIISWebSiteRoot = "IIS://localhost/W3SVC/" + _WebSiteID.ToString() + "/ROOT";
string virtualDirName = _virtualDirName; //虚拟目录名称:Monitor
string physicalPath = _physicalPath; //虚拟目录物理路径 C:\1
if (physicalPath == "")
{
physicalPath = @"D:\Program Files\Macromedia\Flash Media Server 2\applications\MonitorLocal\streams\_definst_\";
}
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
// 如果虚拟目录存在,则删除
foreach (System.DirectoryServices.DirectoryEntry v in root.Children)
{
if (v.Name == _virtualDirName)
{
// Delete the specified virtual directory if it already exists
try
{
root.Invoke("Delete", new string[] { v.SchemaClassName, _virtualDirName });
root.CommitChanges();
}
catch (Exception ex)
{
//sRet += ex.Message;
}
}
}
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, "IIsWebVirtualDir");
tbEntry.Properties["Path"][0] = physicalPath; //虚拟目录物理路径
tbEntry.Invoke("AppCreate", true);
tbEntry.Properties["AccessRead"][0] = true; //设置读取权限
tbEntry.Properties["ContentIndexed"][0] = true;
tbEntry.Properties["DefaultDoc"][0] = "index.aspx,Default.aspx"; //设置默认文档,多值情况下中间用逗号分割
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName; //虚拟目录名称:Monitor
tbEntry.Properties["AccessScript"][0] = true; //执行权限
tbEntry.Properties["AppIsolated"][0] = "1";
tbEntry.Properties["DontLog"][0] = true; // 是否记录日志
//tbEntry.Properties["AuthFlags"][0] = 0; // 设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证
tbEntry.Properties["AuthFlags"][0] = 1;
tbEntry.CommitChanges();
// 增加MIME 的类型:flv
SetMimeTypeProperty(constIISWebSiteRoot, ".flv", "flv");
// 修改IIS属性
SetSingleProperty("IIS://localhost/W3SVC/" + _WebSiteID.ToString(), "ServerBindings", ":8080:");
////得到现默认站点的IP 端口 描述
//string strServerBindings = root.Properties["ServerBindings"].Value.ToString();
////解出端口Port
//string[] strArr = strServerBindings.Split(':');
////重新赋值为8080
//root.Properties["ServerBindings"].Value = strArr[0] + ":8080:" + strArr[2];
root.CommitChanges();//确认更改
return "";
}
catch(Exception ex)
{
//wl.WriteLogMsg("[MonitorService]:创建虚拟目录失败: " + DateTime.Now.ToString() + "\r\n"+ ex.ToString() +"\r\n");
return ex.ToString();
}
}
#region 根据站点名称获取站点ID(标识)
/// <summary>
/// 根据站点名称获取站点ID(标识)
/// </summary>
/// <param name="webSiteName">站点名称</param>
/// <param name="serverIP">主机IP</param>
/// <returns>站点ID</returns>
private int GetSiteID(string webSiteName, string serverIP)
{
int SiteID = 0;
try
{
DirectoryEntry root = new DirectoryEntry(@"IIS://" + serverIP + "/W3SVC");
foreach (DirectoryEntry Child in root.Children)
{
PropertyValueCollection pvc = Child.Properties["ServerBindings"];
string WName = Child.Properties["ServerComment"][0].ToString();
if (webSiteName == WName)
{
SiteID = Convert.ToInt32(Child.Name);
return SiteID;
}
WName = "";
}
}
catch
{
SiteID = 0;
}
return SiteID;
}
#endregion
/// <summary>
/// 设置IIS的MIME类型,SetMimeTypeProperty("IIS://localhost/W3SVC/1/Root", ".hlp", "application/winhlp");
/// </summary>
/// <param name="metabasePath"></param>
/// <param name="newExtension"></param>
/// <param name="newMimeType"></param>
static void SetMimeTypeProperty(string metabasePath, string newExtension, string newMimeType)
{
// metabasePath is of the form "IIS://<servername>/<path>"
// for example "IIS://localhost/W3SVC/1/Root"
// newExtension is of the form ".<extension>", for example, ".hlp"
// newMimeType is of the form "<application>", for example, "application/winhlp"
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 requires a reference to the Active DS IIS Namespace Provider in Visual Studio .NET
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);
}
}
/// <summary>
/// 修改IIS属性
/// </summary>
/// <param name="metabasePath"></param>
/// <param name="propertyName"></param>
/// <param name="newValue"></param>
static void SetSingleProperty(string metabasePath, string propertyName, object newValue)
{
// metabasePath is of the form "IIS://<servername>/<path>"
// for example "IIS://localhost/W3SVC/1"
// propertyName is of the form "<propertyName>", for example "ServerBindings"
// value is of the form "<intStringOrBool>", for example, ":80:"
Console.WriteLine("\nSetting single property at {0}/{1} to {2} ({3}):",
metabasePath, propertyName, newValue, newValue.GetType().ToString());
try
{
DirectoryEntry path = new DirectoryEntry(metabasePath);
PropertyValueCollection propValues = path.Properties[propertyName];
string oldType = propValues.Value.GetType().ToString();
string newType = newValue.GetType().ToString();
Console.WriteLine(" Old value of {0} is {1} ({2})", propertyName, propValues.Value, oldType);
if (newType == oldType)
{
path.Properties[propertyName][0] = newValue;
path.CommitChanges();
Console.WriteLine("Done");
}
else
Console.WriteLine(" Failed in SetSingleProperty; type of new value does not match property");
}
catch (Exception ex)
{
if ("HRESULT 0x80005006" == ex.Message)
Console.WriteLine(" Property {0} does not exist at {1}", propertyName, metabasePath);
else
Console.WriteLine("Failed in SetSingleProperty with the following exception: \n{0}", ex.Message);
}
}
}
}