c#删除iis里面的虚拟目录

guangyang475 2009-04-15 06:52:50

//删除虚拟目录是这样的
public string DelVirtualDirectory(string WebSiteName,string nameDirectory)
{
try
{
string SiteID = GetSiteID(WebSiteName);
if (SiteID == null) return "error:该站点不存在.";

DirectoryEntry deRoot= new DirectoryEntry("IIS://localhost/W3SVC/" + SiteID + "/ROOT");


try
{
DirectoryEntry deVDir = new DirectoryEntry("IIS://localhost/W3SVC/" + SiteID + "/ROOT");
deRoot.RefreshCache();
deVDir = new DirectoryEntry("IIS://localhost/W3SVC/" + SiteID + "/ROOT");
deVDir.Invoke("AppDelete", null);
deRoot.Children.Remove(deVDir);
deRoot.CommitChanges();
deRoot.Close();
return "successful:删除虚拟目录" + nameDirectory + "成功!";
}
catch
{
return "error:该虚拟目录不存在.";
}
}
catch(Exception e)
{
return "error:删除目录失败." + e.Message;
}
}

//查找对应的虚拟站点.
public static string GetSiteID(string WebSiteName)
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
try
{
string SiteID= null;
string hostname;
foreach(DirectoryEntry bb in root.Children)
{
try
{
//if(Convert.ToInt32(bb.Name.Trim()) < 0) continue;
PropertyValueCollection pvc = bb.Properties["ServerBindings"];

String[] srvBindings = ((string)pvc[0]).Split(new char[] {':'});
hostname = srvBindings[2].Trim();

//判断,可换用hostname
//if (WebSiteName == bb.Properties["ServerComment"].Value.ToString()) SiteID=bb.Name;
if (WebSiteName == hostname) SiteID=bb.Name;
// Clear Variable
hostname = "";
}
catch{}
}
if (SiteID == null) return null;
return SiteID;
}
catch
{
return null;
}
}

这里是网上找的2个方法删除虚拟目录,但是为什么在删除默认站点下的虚拟目录找不到这个站点呢
上面那个方法里面有2个参数,第二个参数是做什么用的??
麻烦高手来指点一下或者给个测试通过的方法 谢谢拉
...全文
344 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzbhappy 2010-04-15
  • 打赏
  • 举报
回复
如果删除的是站点,而不是虚似站点,该如何处理,
说明:该站点的端口已经知道,如:8090,站点名:"student"
lxlongnw 2009-05-12
  • 打赏
  • 举报
回复
建议参考下面网站,非常好用:
http://www.cnblogs.com/libiyang/archive/2009/01/04/294455.html
lxlongnw 2009-05-12
  • 打赏
  • 举报
回复
参考下面代码:我验证过完全可以!
1 /// <summary>
2 /// 删除iis虚拟目录
3 /// </summary>
4 /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception>
5 public override void DeleteDirectory()
6 {
7 // 判断目录是否存在
8 if (! this.Exist())
9 {
10 // 若待删除的虚拟目录不存在,则抛出异常
11 DirectoryException.Throw("directory does not exist");
12 }
13
14 try
15 {
16 // 获取上级目录的DirectoryEntry对象
17 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);
18 // 删除参数
19 object[] objParams = new object[2];;
20 objParams[0] = "IIsWebVirtualDir";
21 objParams[1] = this.m_strAlias;
22 // 删除虚拟目录
23 rootEntry.Invoke("Delete", objParams);
24 rootEntry.CommitChanges();
25 }
26 catch (System.Exception ex)
27 {
28 DirectoryException.Throw(ex.Message);
29 }
30
31 }
yafeya 2009-04-15
  • 打赏
  • 举报
回复
deRoot.Children.Remove(deVDir);
改为:
deRoot.Children.Remove(nameDirectory);//nameDirectory虚拟目录别名
wuyq11 2009-04-15
  • 打赏
  • 举报
回复
一参数站点名称,二目录名称
参考
genius_tong 2009-04-15
  • 打赏
  • 举报
回复
sf~

110,580

社区成员

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

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

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