如何从c#中删除IIS对象?
作为卸载方法的一部分,我需要从.NET中删除虚拟目录和应用程序池。我在网上某处发现了以下代码:如何从c#中删除IIS对象?,c#,.net,iis,installation,wmi,C#,.net,Iis,Installation,Wmi,作为卸载方法的一部分,我需要从.NET中删除虚拟目录和应用程序池。我在网上某处发现了以下代码: private static void DeleteTree(string metabasePath) { // metabasePath is of the form "IIS://<servername>/<path>" // for example "IIS://localhost/W3SVC/1/Root/MyVDir"
private static void DeleteTree(string metabasePath)
{
// metabasePath is of the form "IIS://<servername>/<path>"
// for example "IIS://localhost/W3SVC/1/Root/MyVDir"
// or "IIS://localhost/W3SVC/AppPools/MyAppPool"
Console.WriteLine("Deleting {0}:", metabasePath);
try
{
DirectoryEntry tree = new DirectoryEntry(metabasePath);
tree.DeleteTree();
tree.CommitChanges();
Console.WriteLine("Done.");
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("Not found.");
}
}
私有静态void DeleteTree(字符串元数据库路径)
{
//metabasePath的格式为“IIS://”
//例如“IIS://localhost/W3SVC/1/Root/MyVDir”
//或“IIS://localhost/W3SVC/AppPools/MyAppPool”
WriteLine(“删除{0}:”,metabasePath);
尝试
{
DirectoryEntry树=新的DirectoryEntry(metabasePath);
DeleteTree();
tree.CommitChanges();
控制台。WriteLine(“完成”);
}
捕获(DirectoryNotFoundException)
{
Console.WriteLine(“未找到”);
}
}
但它似乎在
tree.CommitChanges()上抛出了一个COMException
代码>。我需要这条线吗?这是一种正确的方法吗?如果要删除应用程序池、虚拟目录或IIS应用程序等对象,则需要按以下方式执行:
string appPoolPath = "IIS://Localhost/W3SVC/AppPools/MyAppPool";
using(DirectoryEntry appPool = new DirectoryEntry(appPoolPath))
{
using(DirectoryEntry appPools =
new DirectoryEntry(@"IIS://Localhost/W3SVC/AppPools"))
{
appPools.Children.Remove(appPool);
appPools.CommitChanges();
}
}
为要删除的项创建一个DirectoryEntry
对象,然后为其父项创建一个DirectoryEntry
。然后告诉父对象删除该对象
您也可以这样做:
string appPoolPath = "IIS://Localhost/W3SVC/AppPools/MyAppPool";
using(DirectoryEntry appPool = new DirectoryEntry(appPoolPath))
{
using(DirectoryEntry parent = appPool.Parent)
{
parent.Children.Remove(appPool);
parent.CommitChanges();
}
}
根据手头的任务,我将使用任何一种方法。你能粘贴完整的COMException吗?你真的应该使用windows安装程序来完成这些事情。Wix具有自动创建和删除IIS对象的内置函数。@Jesse-使用Wix可以提示用户输入新的应用程序池/网站/vdir,而不是选择现有的应用程序池/网站/vdir(VS Web安装项目仅允许您选择现有的IIS对象)?当我有子目录条目时,是否有一种简单的方法可以获取父目录?appPool.Parent可以工作吗?“appPoolpath”vrs“appPoolpath”的大小写问题很小。除此之外,很好answer@simon-良好的定位和固定。助教。