2009-03-19 11 views
10

Piszę klasę instalatora dla mojej usługi internetowej. W wielu przypadkach, gdy używam WMI (np podczas tworzenia katalogów wirtualnych) muszę znać siteid celu zapewnienia prawidłowego metabasePath do witryny, npJak mogę sprawdzić identyfikator witryny IIS w języku C#?

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

Jak mogę to sprawdzić programowo w C#, na podstawie nazwa strony (np. "Domyślna witryna internetowa")?

Odpowiedz

12

Oto jak uzyskać je po nazwie. Możesz modyfikować w razie potrzeby.

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

W moim systemie musiałem zaktualizować powyżej z następujących czynności, aby zmusić go do kompilacji „Wynik = Convert.ToInt32 (site.Name);” – MattH

3
nie

Być może najlepszym sposobem, ale o to sposób:

  1. pętli wszystkich stron pod „IIS: // nazwa_serwera/usługi”
  2. dla każdej ze stron sprawdzić, czy nazwa jest "Default Web Site" w przypadku
  3. jeśli jest prawdziwa to masz swoją id strona

Przykład:

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

Można wyszukiwać miejscu, sprawdzając właściwość ServerComment należącej do dzieci ścieżce metabazy IIS://Localhost/W3SVC że mają SchemaClassName z IIsWebServer.

Poniższy kod demonstruje dwa podejścia:

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); 
     } 
    } 
} 

ta zakłada, że ​​nieruchomość została wykorzystana ServerComment (MMC IIS jego siły stosowane) i jest unikalny.

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

Zwrócony ciąg może zostać sparsowany jako int, jeśli jest to konieczne. Domyślam się, że w większości przypadków nie potrzebujesz go zwracać jako "int", ponieważ użyjesz go do utworzenia 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

Czy działa z wersją IIS <7? Niestety utknąłem z Win2k3 – Grzenio

+0

Ta metoda działa dla IIS6. Użyłem go do znalezienia pul aplikacji. – Helephant

+0

@Helephant, gdzie znaleźć apppools przy użyciu tej metody? w IIS 6 ?? – Kiquenet

Powiązane problemy