您的位置:首页 > 其它

使用ADSI来操作IIS的时的路径

2005-10-08 14:07 357 查看
我们使用ADSI来操作IIS的时候,需要提供他们的Path。比如默认本机80端口的默认站点的目录路径就是:IIS://localhost/w3svc/1/root
它的格式是:
IIS://ComputerName/Service/Website/Directory
ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost
Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是"W3SVC",如果是FTP则应是"MSFTPSVC"
WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,也是数字。
但需要注意的是,并不是自增。后面会有一个小程序获得这个值。
Directory:要操作的目录名称,一个站点一般顶层目录为"ROOT",其它目录则是他的孩子(Child)。

以上资料摘自飞刀的文章,具体看:
http://aspcool.com/lanmu/browse1.asp?ID=914&bbsuser=csharp
http://aspcool.com/lanmu/browse1.asp?ID=915&bbsuser=csharp

由于上面的WebSite 并不是简单的自增。我们要知道某台机子上所有站点对应的值,可以通过下面的小程序获得这个值:
using System.DirectoryServices;
........
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
foreach(DirectoryEntry dir in root.Children)
{
if(dir.SchemaClassName == "IIsWebServer")
{
string ww = dir.Properties["ServerComment"].Value.ToString();
Response.Write (string.Format("IIS://localhost/W3SVC/{0}/ROOT/  {1}<br>",dir.Name,ww));
}
}
当然,你想获得更多的属性值,可以通过 dir.Properties[] 去获得。
为了说明这里的 WebSite 并不是自增的,下面看我在我本机执行上面程序的结果。
IIS://localhost/W3SVC/1/ROOT/ Default Web Site
IIS://localhost/W3SVC/1307630583/ROOT/ MyWeb81
IIS://localhost/W3SVC/1307630584/ROOT/ MyWeb82
IIS://localhost/W3SVC/1307630585/ROOT/ MyWeb83
IIS://localhost/W3SVC/1307630586/ROOT/ MyWeb84
IIS://localhost/W3SVC/1307630587/ROOT/ MyWeb85
IIS://localhost/W3SVC/1307630683/ROOT/ MyWeb90
IIS://localhost/W3SVC/1307630684/ROOT/ MyWeb91
IIS://localhost/W3SVC/1307630685/ROOT/ MyWeb92
IIS://localhost/W3SVC/1307630686/ROOT/ MyWeb93
IIS://localhost/W3SVC/1758797915/ROOT/ ghj1976.net
IIS://localhost/W3SVC/2/ROOT/ Microsoft SharePoint Administration
IIS://localhost/W3SVC/2546/ROOT/ 94
各个的值都不一样。所以在使用这个路径的时候,不要想当然的以为是简单的自增。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
namespace cendy
{
/// <summary>
/// iis 的摘要说明。
/// </summary>
public class iis : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
CreateVDir("sohu.com","sho","d://www",true,true,true,true,true,true,1,"cnsun26");
}
public string CreateVDir(string WebSite, string VDirName,string Path,bool RootDir,bool chkRead,bool chkWrite,bool chkExecute,bool chkScript,bool chkAuth,int webSiteNum,string serverName)
{
string sRet=String.Empty;
System.DirectoryServices.DirectoryEntry IISSchema;
System.DirectoryServices.DirectoryEntry IISAdmin;
System.DirectoryServices.DirectoryEntry VDir;
bool IISUnderNT;
IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/Schema/AppIsolated");
if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN")
IISUnderNT = true;
else
IISUnderNT = false;
IISSchema.Dispose();
IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root");
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)
{
sRet+=ex.Message;
}
}
}
}

if (!RootDir)
{
VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
}
else
{
VDir = IISAdmin;
}
VDir.Properties["AccessRead"][0] = chkRead;
VDir.Properties["AccessExecute"][0] = chkExecute;
VDir.Properties["AccessWrite"][0] = chkWrite;
VDir.Properties["AccessScript"][0] = chkScript;
VDir.Properties["AuthNTLM"][0] = chkAuth;
VDir.Properties["EnableDefaultDoc"][0] = true;
VDir.Properties["EnableDirBrowsing"][0] = false;
VDir.Properties["DefaultDoc"][0] = true;
VDir.Properties["Path"][0] = Path;

if (!IISUnderNT)
{
VDir.Properties["AspEnableParentPaths"][0] = true;
}
VDir.CommitChanges();
if (IISUnderNT)
{
VDir.Invoke("AppCreate", false);
}
else
{
VDir.Invoke("AppCreate", 1);
}
sRet+= "VRoot " +VDirName + " created!";
return sRet;
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐