You are looking for a ServerManager (Microsoft.Web.Administration) that provides read and write access to the IIS 7.0 configuration.
Iterating through Microsoft.Web.Administration.SiteCollection, get a link to your site using the site object and read the value of the Name property.
// Snippet using (ServerManager serverManager = new ServerManager()) { var sites = serverManager.Sites; foreach (Site site in sites) { Console.WriteLine(site.Name); // This will return the WebSite name }
You can also use LINQ to query the ServerManager.Sites collection (see example below)
// Start all stopped WebSites using the power of Linq :) var sites = (from site in serverManager.Sites where site.State == ObjectState.Stopped orderby site.Name select site); foreach (Site site in sites) { site.Start(); }
Note Microsoft.Web.Administration only works with IIS7 .
For IIS6, you can use both ADSI and WMI, but I suggest you upgrade to WMI, which is faster than ADSI. If you are using WMI, check out WMI Code Creator 1.0 (free / developed by Microsoft). It will generate code for you.
NTN
ntze Nov 01 '09 at 13:51 2009-11-01 13:51
source share