Showing posts with label IIS. Show all posts
Showing posts with label IIS. Show all posts

Friday, April 20, 2018

GET SiteName , ApplicationPoolName From IIS

You are looking for ServerManager (Microsoft.Web.Administration) which provides read and write access to the IIS 7.0 configuration system.

(FIND in Forlder : C:\Windows\System32\inetsrv )


// 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 works only with IIS7.
And  RUN VisualStudio  with RUN AS ADMINISTRATOR

===

Với  ApplicationPool

 using (ServerManager serverManager = new ServerManager())
            {
                var appPools = serverManager.ApplicationPools;

      foreach (ApplicationPool appPool in appPools)
                {
                    Console.WriteLine(appPool.Name)

                }

}

IIS7 :Adding or Deleting a website programmatically - C# Example

You can add a website using Add method that is present in the Sites collection of ServerManager. Below is the code snippet which creates an application Pool, and then creates a site, also enables the FREB and commit the changes to the IIS7 configuration file:
ServerManager serverMgr = new ServerManager();
Site mySite = serverMgr.Sites.Add("MySiteName", "C:\\inetpub\\wwwroot", 8080);
serverMgr.ApplicationPools.Add("MyAppPool");
mySite.ApplicationDefaults.ApplicationPoolName = "MyAppPool";
mySite.TraceFailedRequestsLogging.Enabled = true;
mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site";
serverMgr.CommitChanges();
Now, let's try to delete the same website created. As you know earlier, in IIS7, you need to give an unique name for each website (which was not the case in IIS 6.0).
Site s1 = serverMgr.Sites["MySiteName"]; // you can pass the site name or the site ID
serverMgr.Sites.Remove(s1);
serverMgr.CommitChanges();
That was very simple correct? what are you waiting for? ExploreMicrosoft.Web.Administration.dll more. You will love it! Again, IIS7 is the best friend to the developers!