[英]How to use DirectoryEntry(“IIS://Localhost/W3SVC”) to change IP address with several host header entries
我正在嘗試以編程方式更改在Server 2003上運行的網站的IP。運行以下代碼時,網站綁定本身會更改,但是還有其他主機頭值需要更改。 我該如何更改?
protected static void ChangeServerIP(string old_ip, string new_ip)
{
int siteChangedCount = 0;
DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/W3SVC");
if (entry != null)
{
foreach (DirectoryEntry site in entry.Children)
{
if (site.SchemaClassName == "IIsWebServer")
{
Console.WriteLine("Server Binding: " + site.Properties["ServerBindings"][0].ToString());
if (site.Properties["ServerBindings"][0].ToString().Contains(old_ip))
{
string ServerComment = site.Properties["ServerComment"].Value.ToString();
Console.WriteLine("Changing " + ServerComment + "'s IP address from " + old_ip + " to " + new_ip);
site.Properties["ServerBindings"].Value = site.Properties["ServerBindings"][0].ToString().Replace(old_ip, new_ip);
site.CommitChanges();
siteChangedCount++;
Console.WriteLine("New IP address bound to site: " + ServerComment + " IP: " + site.Properties["ServerBindings"].Value.ToString());
}
}
}
}
}
您是否嘗試將值分配給PropertyValueCollection中的第一項?
site.Properties["ServerBindings"][0].Value = site.Properties["ServerBindings"][0].ToString().Replace(old_ip, new_ip);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.