2010-07-23 74 views
2

我試圖設置一個安裝程序來註冊一個網站。目前,我已經在Windows Server 2003下創建了一個應用程序池和網站。不幸的是,每當我嘗試修改ServerBindings屬性來設置IP地址時,它都會引發一個異常。我第一次嘗試這是因爲這裏的文檔告訴我http://msdn.microsoft.com/en-us/library/ms525712%28VS.90%29.aspx。我目前使用VB.NET,但C#的答案也可以,因爲我需要將它切換到使用C#。以編程方式設置IIS 6.0的服務器綁定

siteRootDE.Properties.Item("ServerBindings").Item(0) = "<address>" 

這引發了一個ArgumentOutOfRangeException。我檢查了它,並且服務器綁定是大小爲0。當我試圖在列表中創建一個像這樣的新條目:

siteRootDE.Properties.Item("ServerBindings").Add("<address>") 

我得到一個收到COMException當我嘗試這一點。

我看着註冊的屬性鍵,ServerBindings無處可尋。但是,當我通過IIS創建網站時,它會正確生成ServerBindings,並且我可以看到它。

我需要做些什麼才能讓ServerBindings出現?

編輯:我將代碼移到C#並嘗試它。看起來由於某種原因,VB.NET在給出上述情況時會崩潰,但C#不會。但是,該代碼似乎還沒有做任何事情。它只是默默地失敗。我想這樣的:

// WebPage is the folder where I created the website 
DirectoryEntry siteRootDE = new DirectoryRoot("IIS://LocalHost/W3SVC/WebPage"); 
// www.mydomain.com is one of the IP addresses that shows up 
// when I used the IIS administrative program 
siteRootDE.Properties["ServerBindings"].Value = ":80:www.mydomain.com"; 
siteRootDE.CommitChanges(); 

回答

5

在C#中,你應該能夠做到這一點:

webSite.Invoke("Put", "ServerBindings", ":80:www.mydomain.com"); 

webSite.Properties["ServerBindings"].Value = ":80:www.mydomain.com"; 

編輯:

下面是示例我使用的代碼。

public static void CreateNewWebSite(string siteID, string hostname) 
{ 
    DirectoryEntry webService = new DirectoryEntry("IIS://LOCALHOST/W3SVC"); 

    DirectoryEntry website = new DirectoryEntry(); 
    website = webService.Children.Add(siteID, "IIsWebServer"); 
    website.CommitChanges(); 

    website.Invoke("Put", "ServerBindings", ":80:" + hostname); 
    // Or website.Properties["ServerBindings"].Value = ":80:" + hostname;    
    website.Properties["ServerState"].Value = 2; 
    website.Properties["ServerComment"].Value = hostname; 
    website.CommitChanges(); 

    DirectoryEntry rootDir = website.Children.Add("ROOT", "IIsWebVirtualDir"); 
    rootDir.CommitChanges(); 

    rootDir.Properties["AppIsolated"].Value = 2; 
    rootDir.Properties["Path"].Value = @"C:\Inetpub\wwwroot\MyRootDir"; 
    rootDir.Properties["AuthFlags"].Value = 5; 
    rootDir.Properties["AccessFlags"].Value = 513; 
    rootDir.CommitChanges(); 
    website.CommitChanges(); 
    webService.CommitChanges(); 
} 

此外,這裏是一個很好article作爲參考。

+0

不幸的是,沒有奏效。它仍然沒有顯示在IIS中。 – 2010-07-27 18:01:58

+0

這很有趣。此代碼來自工作程序。如果你喜歡,我可以發佈整個示例代碼? – Garett 2010-07-27 18:28:49

+0

最終工作。它只顯示在IIS中,如果我按「高級」,但它在那裏。我相信我的問題可能是在rootDir而不是網站上設置了屬性。 – 2010-07-28 13:04:16

相關問題