C#本地iis建立虚拟目录
来源:互联网 发布:app直播源码 编辑:程序博客网 时间:2024/06/05 18:43
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
/**
* 贺清华
* */
namespace Create
{
public class IISManager
{
public IISManager()
{
}
public static string VirDirSchemaName = "IIsWebVirtualDir";
private string _serverName;
public string ServerName
{
get
{
return _serverName;
}
set
{
_serverName = value;
}
}
#region 创建網站或虚拟目录
/// <summary>
/// 创建網站或虚拟目录
/// </summary>
/// <param name="WebSite">服务器站点名称(localhost)</param>
/// <param name="VDirName">虚拟目录名称</param>
/// <param name="Path">绝对路径</param>
/// <param name="RootDir">true=网站;false=虚拟</param>
/// <param name="iAuth">设置目录的安全性,0不允许匿名访问,1为允许,2基本身份验证,3允许匿名+基本身份验证,4整合Windows驗證,5允许匿名+整合Windows驗證...</param>
/// <param name="webSiteNum">站点编号</param>
/// <param name="serverName">一般為localhost</param>
/// <returns></returns>
public bool CreateWebSite(string WebSite, string VDirName, string Path, bool RootDir, int iAuth, int webSiteNum, string serverName)
{
try
{
System.DirectoryServices.DirectoryEntry IISSchema;
System.DirectoryServices.DirectoryEntry IISAdmin;
System.DirectoryServices.DirectoryEntry VDir;
bool IISUnderNT;
//
// 确定IIS版本
//
IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated");
if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN")
IISUnderNT = true;
else
IISUnderNT = false;
IISSchema.Dispose();
//
// Get the admin object
// 获得管理权限
//
IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/W3SVC/" + webSiteNum + "/Root");
//
// If we're not creating a root directory
// 如果我们不能创建一个根目录
//
if (!RootDir)
{
//
// 如果虚拟目录已经存在则删除
//
foreach (System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
{
if (v.Name == VDirName)
{
// 删除
try
{
IISAdmin.Invoke("Delete", new string[] { v.SchemaClassName, VDirName });
IISAdmin.CommitChanges();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
//
// 创建一个虚拟目录
//
if (!RootDir)
{
VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
}
else
{
VDir = IISAdmin;
}
//
// 创建一个web应用
//
if (IISUnderNT)
{
VDir.Invoke("AppCreate", false);
}
else
{
VDir.Invoke("AppCreate", true);
}
//
// 安装虚拟目录
VDir.Properties["AppFriendlyName"][0] = VDirName; //应用程序名称
VDir.Properties["AccessRead"][0] = true; //设置读取权限
VDir.Properties["AccessExecute"][0] = false;
VDir.Properties["AccessWrite"][0] = false;
VDir.Properties["AccessScript"][0] = true; //执行权限[純腳本]
VDir.Properties["EnableDefaultDoc"][0] = true;
VDir.Properties["EnableDirBrowsing"][0] = false;
VDir.Properties["DefaultDoc"][0] = "Default.aspx,Index.aspx,Index.asp"; //设置默认文档,多值情况下中间用逗号分割
VDir.Properties["Path"][0] = Path;
VDir.Properties["AuthFlags"][0] = iAuth;
//
// NT格式不支持这特性
//
if (!IISUnderNT)
{
VDir.Properties["AspEnableParentPaths"][0] = true;
}
//
// 设置改变
//
VDir.CommitChanges();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
/// <summary>
/// 獲取VDir支持的所有屬性
/// </summary>
/// <returns></returns>
public string GetVDirPropertyName()
{
const String constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT/iKaoo";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
string sOut = "";
//下面的方法是得到所有属性名称的方法:
foreach (PropertyValueCollection pvc in root.Properties)
{
//Console.WriteLine(pvc.PropertyName);
sOut += pvc.PropertyName + ":" + pvc.Value.ToString() + "-----------";
}
return sOut;
}
/// <summary>
/// 創建虛擬目錄
/// </summary>
/// <param name="sDirName">虛擬目錄程式名稱</param>
/// <param name="sPath">實體路徑</param>
/// <param name="sDefaultDoc">黙認首頁,多個名稱用逗號分隔</param>
/// <param name="iAuthFlags">设置目录的安全性,0不允许匿名访问,1为允许,2基本身份验证,3允许匿名+基本身份验证,4整合Windows驗證,5允许匿名+整合Windows驗證...更多請查閱MSDN</param>
/// <param name="sWebSiteNumber">Win2K,2K3支持多個網站,本次操作哪個網站,黙認網站為1</param>
/// <returns></returns>
public bool CreateVDir(string sDirName,string sPath,string sDefaultDoc,int iAuthFlags,string sWebSiteNumber)
{
try
{
String sIISWebSiteRoot = "IIS://localhost/W3SVC/" + sWebSiteNumber + "/ROOT";
DirectoryEntry root = new DirectoryEntry(sIISWebSiteRoot);
foreach (System.DirectoryServices.DirectoryEntry v in root.Children)
{
if (v.Name == sDirName)
{
// Delete the specified virtual directory if it already exists
root.Invoke("Delete", new string[] { v.SchemaClassName, sDirName });
root.CommitChanges();
}
}
DirectoryEntry tbEntry = root.Children.Add(sDirName, root.SchemaClassName);
tbEntry.Properties["Path"][0] = sPath;
tbEntry.Invoke("AppCreate", true);
//tbEntry.Properties["AccessRead"][0] = true;
//tbEntry.Properties["ContentIndexed"][0] = true;
tbEntry.Properties["DefaultDoc"][0] = sDefaultDoc;
tbEntry.Properties["AppFriendlyName"][0] = sDirName;
//tbEntry.Properties["AccessScript"][0] = true;
//tbEntry.Properties["DontLog"][0] = true;
//tbEntry.Properties["AuthFlags"][0] = 0;
tbEntry.Properties["AuthFlags"][0] = iAuthFlags;
tbEntry.CommitChanges();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 删除虚拟目录
/// </summary>
/// <param name="WebSiteName">站点名称</param>
/// <param name="nameDirectory">虚拟目录名</param>
/// <returns>0站点不存在,1成功,2失败+异常</returns>
public int DelVirtualDirectory(string WebSiteName, string nameDirectory)
{
try
{
string SiteID = IISAdminLib.GetWebSiteNum(WebSiteName);
if (SiteID == null) return 0;
DirectoryEntry deRoot = new DirectoryEntry("IIS://localhost/W3SVC/" + SiteID + "/ROOT");
deRoot.RefreshCache();
foreach(DirectoryEntry ee in deRoot.Children)
{
if (ee.Name == nameDirectory)
{
deRoot.Children.Remove(ee);
deRoot.CommitChanges();
deRoot.Close();
break;
}
}
return 1;
}
catch
{
return 2;
}
}
}
}
- C#本地iis建立虚拟目录
- 实现IIS中建立虚拟目录
- 使用共享资源建立IIS虚拟目录
- 使用c#建立虚拟目录
- 使用c#建立虚拟目录
- c#建立虚拟目录
- 编程建立IIS站点和虚拟目录
- C#在iis里添加虚拟目录
- C#代码操作IIS之虚拟目录
- C# 获取IIS站点及虚拟目录信息
- C#创建IIS站点或者虚拟目录
- C# 获取IIS站点及虚拟目录信息
- C# 获取IIS站点及虚拟目录信息
- 编程建立IIS站点和虚拟目录(1)(头文件)
- 编程建立IIS站点和虚拟目录(2)(实现文件)
- .net程序建立并设置IIS站点或虚拟目录
- 在IIS(Internet 信息服务)中建立一个虚拟目录
- IIS下建立负载均衡的虚拟目录方法
- 工作<=应届生
- 如何实现ubuntu桌面远程控制?
- xml文件对比或xml大字符串对比方法(蛮精简的)
- JavaScript 记录页面停留时间-通过测试
- DateTime.Now不准确的问题
- C#本地iis建立虚拟目录
- [双语阅读]Unfriend当选2009年度潮词
- WinCE应用程序部分学习总结
- 横屏,竖屏
- 解决JDeveloper 11g无法启动
- In my face 咄咄逼人
- php对图片添加水印类
- 免费+收费模式 freemium
- [英语阅读]经济趋平稳 感恩节出行增加