如何从c#中删除IIS对象?
发布时间:2020-12-15 08:09:05 所属栏目:百科 来源:网络整理
导读:作为卸载方法的一部分,我需要从.NET中删除虚拟目录和应用程序池.我在网上找到了以下代码: private static void DeleteTree(string metabasePath) { // metabasePath is of the form "IIS://servername/path" // for example "IIS://localhost/W3SVC/1/Root/
作为卸载方法的一部分,我需要从.NET中删除虚拟目录和应用程序池.我在网上找到了以下代码:
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."); } } 但它似乎在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(); } } 根据手头的任务,我会使用任何一种方法. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |