C#实现获取IIS站点及虚拟目录信息的方法

来源:互联网 发布:幼儿园打卡软件 编辑:程序博客网 时间:2024/05/22 00:46

这篇文章主要介绍了C#实现获取IIS站点及虚拟目录信息的方法,可实现获取IIS站点信息及物理路径等功能,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#实现获取IIS站点及虚拟目录信息的方法。分享给大家供大家参考。具体如下:

 

  1. using System; 
  2. using System.DirectoryServices; 
  3. using System.Collections.Generic; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. class Program 
  7. static void Main(string[] args) 
  8. DirectoryEntry rootEntry = new DirectoryEntry("IIS://localhost/w3svc"); 
  9. int siteID = 1; 
  10. foreach (DirectoryEntry entry in rootEntry.Children) 
  11. if (entry.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase)) 
  12. Console.WriteLine("Name: {0}", entry.Name); 
  13. Console.WriteLine("Path: {0}", IISWorker.GetWebsitePhysicalPath(entry)); 
  14. Console.WriteLine("ServerBindings: {0}", entry.Properties["ServerBindings"].Value); 
  15. Console.WriteLine(); 
  16. DirectoryEntry virEntry = new DirectoryEntry(entry.Path + "/ROOT"); 
  17. foreach (DirectoryEntry entryVirtual in virEntry.Children) 
  18. if (entryVirtual.SchemaClassName.Equals("IIsWebVirtualDir", StringComparison.OrdinalIgnoreCase)) 
  19. Console.WriteLine("SchemaClassName: {0}", entryVirtual.SchemaClassName); 
  20. Console.WriteLine("Name: {0}", entryVirtual.Name); 
  21. Console.WriteLine("Path: {0}", entryVirtual.Properties["Path"].Value); 
  22. Console.WriteLine(); 
  23. int ID = Convert.ToInt32(entry.Name); 
  24. if (ID >= siteID) 
  25. siteID = ID + 1; 
  26. public class IISWorker 
  27. /// <summary> 
  28. /// 得到网站的物理路径 
  29. /// </summary> 
  30. /// <param name="rootEntry">网站节点</param> 
  31. /// <returns></returns> 
  32. public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry) 
  33. string physicalPath = ""
  34. foreach (DirectoryEntry childEntry in rootEntry.Children) 
  35. if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root")) 
  36. if (childEntry.Properties["Path"].Value != null
  37. physicalPath = childEntry.Properties["Path"].Value.ToString(); 
  38. else 
  39. physicalPath = ""
  40. return physicalPath; 

希望本文所述对大家的C#程序设计有所帮助。

原创粉丝点击