如何实现下列功能?

f_h_x_ 2004-12-01 05:33:54
C#如何操作IIS?如何用代码建虚拟目录?

在线等>分不够再加.
...全文
55 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
沈逸 2004-12-01
  • 打赏
  • 举报
回复
public static bool EnsureNewSiteEnavaible(string bindStr)

{

string entPath = String.Format("IIS://{0}/w3svc", HostName);

DirectoryEntry ent = GetDirectoryEntry(entPath);



foreach(DirectoryEntry child in ent.Children)

{

if(child.SchemaClassName == "IIsWebServer")

{

if(child.Properties["ServerBindings"].Value != null)

{

if(child.Properties["ServerBindings"].Value.ToString() == bindStr)

{

return false;

}

}

}

}



return true;

}

#endregion



#region 获取一个网站编号的方法

/// <summary>

/// 获取一个网站的编号。根据网站的ServerBindings或者ServerComment来确定网站编号

/// </summary>

/// <param name="siteName"></param>

/// <returns>返回网站的编号</returns>

/// <exception cref="NotFoundWebSiteException">表示没有找到网站</exception>

public static string GetWebSiteNum(string siteName)

{

Regex regex = new Regex(siteName);

string tmpStr;



string entPath = String.Format("IIS://{0}/w3svc", HostName);

DirectoryEntry ent = GetDirectoryEntry(entPath);



foreach(DirectoryEntry child in ent.Children)

{

if(child.SchemaClassName == "IIsWebServer")

{

if(child.Properties["ServerBindings"].Value != null)

{

tmpStr = child.Properties["ServerBindings"].Value.ToString();

if(regex.Match(tmpStr).Success)

{

return child.Name;

}

}



if(child.Properties["ServerComment"].Value != null)

{

tmpStr = child.Properties["ServerComment"].Value.ToString();

if(regex.Match(tmpStr).Success)

{

return child.Name;

}

}

}

}



throw new NotFoundWebSiteException("没有找到我们想要的站点" + siteName);

}

#endregion

沈逸 2004-12-01
  • 打赏
  • 举报
回复
/// <summary>

/// 删除一个网站。根据网站名称删除。

/// </summary>

/// <param name="siteName">网站名称</param>

public static void DeleteWebSiteByName(string siteName)

{

string siteNum = GetWebSiteNum(siteName);

string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);

DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);



string rootPath = String.Format("IIS://{0}/w3svc", HostName);

DirectoryEntry rootEntry = GetDirectoryEntry(rootPath);



rootEntry.Children.Remove(siteEntry);

rootEntry.CommitChanges();

}

#endregion



#region Start和Stop网站的方法

public static void StartWebSite(string siteName)

{

string siteNum = GetWebSiteNum(siteName);

string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);

DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);



siteEntry.Invoke("Start", new object[] {});

}



public static void StopWebSite(string siteName)

{

string siteNum = GetWebSiteNum(siteName);

string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);

DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);



siteEnt
沈逸 2004-12-01
  • 打赏
  • 举报
回复
public static DirectoryEntry GetDirectoryEntry(string entPath)

{

DirectoryEntry ent;



if(UserName == null)

{

ent = new DirectoryEntry(entPath);

}

else

{

// ent = new DirectoryEntry(entPath, HostName+"\\"+UserName, Password, AuthenticationTypes.Secure);

ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);

}



return ent;

}

#region 添加,删除网站的方法

/// <summary>

/// 创建一个新的网站。根据传过来的信息进行配置

/// </summary>

/// <param name="siteInfo">存储的是新网站的信息</param>

public static void CreateNewWebSite(NewWebSiteInfo siteInfo)

{

if(! EnsureNewSiteEnavaible(siteInfo.BindString))

{

throw new DuplicatedWebSiteException("已经有了这样的网站了。" + Environment.NewLine + siteInfo.BindString);

}



string entPath = String.Format("IIS://{0}/w3svc", HostName);

DirectoryEntry rootEntry = GetDirectoryEntry(entPath);



string newSiteNum = GetNewWebSiteID();

DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer");

newSiteEntry.CommitChanges();



newSiteEntry.Properties["ServerBindings"].Value = siteInfo.BindString;

newSiteEntry.Properties["ServerComment"].Value = siteInfo.CommentOfWebSite;

newSiteEntry.CommitChanges();



DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");

vdEntry.CommitChanges();



vdEntry.Properties["Path"].Value = siteInfo.WebPath;

vdEntry.CommitChanges();

}

nga96 2004-12-01
  • 打赏
  • 举报
回复
分太少了些吧,呵
代码我有,不知如何给你呢

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧