软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP教程 > ADSI实现IIS管理 WEB站点管理系统核心代码

ADSI实现IIS管理 WEB站点管理系统核心代码

//以下是站点管理系统的一些核心代码,也来自网上的一些朋友的资料,七拼八凑,也缺乏测试

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.DirectoryServices;
using IISMModel;
using System.Configuration;
using System.Web;

namespace IISMBLL
{
/// <summary>
/// IIS站点管理器
/// </summary>
public class IISManager
{
const string W3SVC_PATH = "IIS://localhost/W3SVC";
// Access Flags
const int MD_ACCESS_READ = 0x00000001; //Allow read access.
const int MD_ACCESS_SCRIPT = 0x00000200; // Allow script execution.

/// <summary>
/// 验证某个虚拟目录是否存在
/// </summary>
/// <param name="baseSiteName">虚拟目录所在的基础站点</param>
/// <param name="virtualDirName">虚拟目录名</param>
/// <returns>存在返回真,否则返回假</returns>
public Boolean ValidateVirWebSite(string baseSiteName,string virtualDirName)
{
if (string.IsNullOrEmpty(baseSiteName))
{
throw new Exception(IISModel_MSG.IISBLL_IISM_ERROR_VIRDIR_SITE_EMPTY);
}

if (string.IsNullOrEmpty(virtualDirName))
{
throw new Exception(IISModel_MSG.IISBLL_IISM_ERROR_VIRDIR);
}

// 站点ID
int _WebSiteID = 1;
_WebSiteID = GetSiteID(baseSiteName, "localhost");
if (_WebSiteID == 0)
{
throw new Exception(IISModel_MSG.IISBLL_IISM_ERROR_VIRDIR_SITE_NOTFOUND);
}

String siteRoot = "IIS://localhost/W3SVC/" + _WebSiteID.ToString() + "/ROOT";
DirectoryEntry root = new DirectoryEntry(siteRoot);
foreach (DirectoryEntry child in root.Children)
{
if (child.Name.ToLower() == virtualDirName)
{
return true;
}
}
return false;
}

/// <summary>
/// 验证某个站点是否存在
/// </summary>
/// <param name="siteName"></param>
/// <returns></returns>
public Boolean ValidateWebSite(string siteName)
{
return
GetSiteID(siteName, "localhost")>0;
}

/// <summary>
/// 在主机上创建虚拟目录
/// </summary>
public void CreateVirWebSite(string _virtualDirName, string _physicalPath)
{
try
{
// 站点ID
int _WebSiteID = 1;
//得到默认站点
_WebSiteID = GetSiteID(ConfigurationManager.AppSettings["defaultSite"], "localhost");
if (_WebSiteID == 0)
{
_WebSiteID = 1;
}
if (string.IsNullOrEmpty(_virtualDirName))
{
throw new NullReferenceException("创建虚拟目录名不能为空");
}

String constIISWebSiteRoot = "IIS://localhost/W3SVC/" + _WebSiteID.ToString() + "/ROOT";
string virtualDirName = _virtualDirName; //虚拟目录名称:Monitor
string physicalPath = _physicalPath; //虚拟目录物理路径

if (String.IsNullOrEmpty(physicalPath))
{
throw new NullReferenceException("创建虚拟目录的物理路径不能为空");
}

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
{
throw;
}
}
}

相关阅读
网友评论