如何用C#删除IIS中的站点?在线等

KeNan_Net KeNan_Net 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//删除虚拟目录是这样的,但站点怎么做呢?
public static 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;
}
}


这里是删除虚拟目录的方法,那删除站点怎么做?
...全文

相关推荐

评论 13
收藏 已收藏
分享
返回顶部
KeNan_Net
.NET社区 62,272 成员 668,968 内容