2009-03-19 18 views
10

Tôi đang viết một lớp trình cài đặt cho dịch vụ web của mình. Trong nhiều trường hợp khi tôi sử dụng WMI (ví dụ như khi tạo thư mục ảo) Tôi phải biết SiteID để cung cấp các metabasePath đúng vào trang web, ví dụ:Làm cách nào tôi có thể tra cứu id trang IIS trong C#?

metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]" 
for example "IIS://localhost/W3SVC/1/Root" 

Làm thế nào tôi có thể nhìn nó lên lập trình trong C#, dựa trên tên của trang web (ví dụ: "Trang web mặc định")?

Trả lời

12

Đây là cách làm theo tên. Bạn có thể sửa đổi nếu cần.

public int GetWebSiteId(string serverName, string websiteName) 
{ 
    int result = -1; 

    DirectoryEntry w3svc = new DirectoryEntry(
         string.Format("IIS://{0}/w3svc", serverName)); 

    foreach (DirectoryEntry site in w3svc.Children) 
    { 
    if (site.Properties["ServerComment"] != null) 
    { 
     if (site.Properties["ServerComment"].Value != null) 
     { 
     if (string.Compare(site.Properties["ServerComment"].Value.ToString(), 
          websiteName, false) == 0) 
     { 
      result = int.Parse(site.Name); 
      break; 
     } 
     } 
    } 
    } 

    return result; 
} 
+2

Trên hệ thống của tôi, tôi đã phải cập nhật trên bằng những điều sau để có được nó để biên dịch "result = Convert.ToInt32 (site.Name);" – MattH

3

Có lẽ không phải là cách tốt nhất, nhưng đây là một cách:

  1. lặp qua tất cả các trang web dưới "IIS: // servername/dịch vụ"
  2. cho mỗi người trong số các trang web kiểm tra xem tên của nó là "Default Web site" trong trường hợp của bạn
  3. nếu đúng thì bạn có trang web của bạn id

Ví dụ:

Dim oSite As IADsContainer 
Dim oService As IADsContainer 
Set oService = GetObject("IIS://localhost/W3SVC") 
For Each oSite In oService 
    If IsNumeric(oSite.Name) Then 
     If oSite.ServerComment = "Default Web Site" Then 
      Debug.Print "Your id = " & oSite.Name 
     End If 
    End If 
Next 
5

Bạn có thể tìm kiếm một trang web bằng cách kiểm tra các ServerComment tài sản thuộc về con cái của con đường metabase IIS://Localhost/W3SVC rằng có một SchemaClassName của IIsWebServer.

Ví dụ dưới đây hai phương pháp:

string siteToFind = "Default Web Site"; 

// The Linq way 
using (DirectoryEntry w3svc1 = new DirectoryEntry("IIS://Localhost/W3SVC")) 
{ 
    IEnumerable<DirectoryEntry> children = 
      w3svc1.Children.Cast<DirectoryEntry>(); 

    var sites = 
     (from de in children 
     where 
      de.SchemaClassName == "IIsWebServer" && 
      de.Properties["ServerComment"].Value.ToString() == siteToFind 
     select de).ToList(); 
    if(sites.Count() > 0) 
    { 
     // Found matches...assuming ServerComment is unique: 
     Console.WriteLine(sites[0].Name); 
    } 
} 

// The old way 
using (DirectoryEntry w3svc2 = new DirectoryEntry("IIS://Localhost/W3SVC")) 
{ 

    foreach (DirectoryEntry de in w3svc2.Children) 
    { 
     if (de.SchemaClassName == "IIsWebServer" && 
      de.Properties["ServerComment"].Value.ToString() == siteToFind) 
     { 
      // Found match 
      Console.WriteLine(de.Name); 
     } 
    } 
} 

này giả định rằng các ServerComment bất động sản đã được sử dụng (lực lượng IIS MMC sử dụng của nó) và là duy nhất.

3
private static string FindWebSiteByName(string serverName, string webSiteName) 
{ 
    DirectoryEntry w3svc = new DirectoryEntry("IIS://" + serverName + "/W3SVC"); 
    foreach (DirectoryEntry site in w3svc.Children) 
    { 
     if (site.SchemaClassName == "IIsWebServer" 
      && site.Properties["ServerComment"] != null 
      && site.Properties["ServerComment"].Value != null 
      && string.Equals(webSiteName, site.Properties["ServerComment"].Value.ToString(), StringComparison.OrdinalIgnoreCase)) 
     { 
      return site.Name; 
     } 
    } 

    return null; 
} 
+0

Chuỗi được trả về có thể được phân tích cú pháp dưới dạng int nếu cần. Tôi đoán là trong hầu hết các trường hợp, bạn không thực sự cần nó trở lại như một 'int' như bạn sẽ sử dụng nó để tạo một URI. – CodeMonkeyKing

3
public static ManagementObject GetWebServerSettingsByServerComment(string serverComment) 
     { 
      ManagementObject returnValue = null; 

      ManagementScope iisScope = new ManagementScope(@"\\localhost\root\MicrosoftIISv2", new ConnectionOptions()); 
      iisScope.Connect(); 
      if (iisScope.IsConnected) 
      { 
       ObjectQuery settingQuery = new ObjectQuery(String.Format(
        "Select * from IIsWebServerSetting where ServerComment = '{0}'", serverComment)); 

       ManagementObjectSearcher searcher = new ManagementObjectSearcher(iisScope, settingQuery); 
       ManagementObjectCollection results = searcher.Get(); 

       if (results.Count > 0) 
       { 
        foreach (ManagementObject manObj in results) 
        { 
         returnValue = manObj; 

         if (returnValue != null) 
         { 
          break; 
         } 
        } 
       } 
      } 

      return returnValue; 
     } 
+0

Tính năng này có hoạt động với phiên bản IIS <7 không? Thật không may tôi đang mắc kẹt với Win2k3 – Grzenio

+0

Phương pháp này hoạt động cho IIS6. Tôi đã sử dụng nó để tìm hồ bơi ứng dụng. – Helephant

+0

@Helephant, nơi tìm apppools bằng cách sử dụng phương pháp này ?? trong IIS 6 ?? – Kiquenet

Các vấn đề liên quan