/// <summary> /// Adds a host header value to a specified website. WARNING: NO ERROR CHECKING IS PERFORMED IN THIS EXAMPLE. /// YOU ARE RESPONSIBLE FOR THAT EVERY ENTRY IS UNIQUE /// </summary> /// <param name="hostHeader">The host header. Must be in the form IP:Port:Hostname </param> /// <param name="websiteID">The ID of the website the host header should be added to </param> public static void AddHostHeader(string hostHeader, string websiteID) { DirectoryEntry site = new DirectoryEntry("IIS://localhost/w3svc/" + websiteID); try { //Get everything currently in the serverbindings propery. PropertyValueCollection serverBindings = site.Properties["ServerBindings"]; //Add the new binding serverBindings.Add(hostHeader); //Create an object array and copy the content to this array Object [] newList = new Object[serverBindings.Count]; serverBindings.CopyTo(newList, 0); //Write to metabase site.Properties["ServerBindings"].Value = newList; //Commit the changes site.CommitChanges(); } catch (Exception e) { Console.WriteLine(e); } }
public bool AddHostHeader(string hostHeader) { if (hostHeader.IndexOf(':') < 0) { return(false); } using (DirectoryEntry site = new DirectoryEntry("IIS://" + _serverName + "/w3svc/" + GetWebSiteId())) { try { PropertyValueCollection serverBindings = site.Properties["ServerBindings"]; if (ExistHostHeader(hostHeader)) { return(false); } serverBindings.Add(hostHeader); Object[] newList = new Object[serverBindings.Count]; serverBindings.CopyTo(newList, 0); site.Properties["ServerBindings"].Value = newList; site.CommitChanges(); return(true); } catch { return(false); } } }
public static string[] GetPropertyValueAsStringArray(DirectoryEntry entry, string property) { PropertyValueCollection values = GetPropertyValues(entry, property); string[] array = new string[values.Count]; values.CopyTo(array, 0); return(array); }