2009-03-19 6 views
10

私はWebサービス用のインストーラクラスを作成しています。私は、C#でプログラム的にそれを見ることができますどのように、上のベースC#でIISサイトIDを検索するにはどうすればよいですか?

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

:私はWMI(例えば仮想ディレクトリを作成する)を使用する多くの場合、私は、サイトへの正しいmetabasePath、例えばを提供するのsiteIdを知っている必要がありますサイトの名前(例: "Default Web Site")?

たぶん

答えて

12

名前で取得する方法は次のとおりです。必要に応じて変更することができます。

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

が、私はそれをコンパイルするために取得するために、次で上記を更新しなければならなかった「結果= Convert.ToInt32(site.Name);」 – MattH

3

ない最良の方法が、ここでは道である:「IIS://サーバー/サービス」の下にあるすべてのサイトを通じて

  1. ループ名がある場合はサイトごとに
  2. をチェックあなたのケースでは、 "既定のWebサイト"
  3. trueの場合は、あなたが持っているサイトのID

例:

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

あなたはSchemaClassNameIIsWebServerのを持っているメタベースパスIIS://Localhost/W3SVCの子どもたちに属するServerCommentプロパティを調べることで、サイトを検索することができます。

次のコードは、2つのアプローチを示しています

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

これはServerCommentプロパティを使用(IISのMMC力はその使用)と一意であるされていることを前提としています。私のシステムで

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

返される文字列は、必要であればintとして解析できます。私の推測では、ほとんどの場合、URIを作成するために使用するので、実際には 'int'として返される必要はありません。 – 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

IISバージョン<7で動作しますか?残念ながら私はWin2k3で立ち往生しています – Grzenio

+0

この方法はIIS6で機能します。私はそれを使用してアプリケーションプールを見つけました。 – Helephant

+0

@Helephant、ここでこの方法を使ってapppoolsを見つけるのですか? IIS 6で? – Kiquenet

関連する問題