2010-07-23 61 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

相关问题