Working with IIS 7 Programmetically using C#

Pre-Requisite

  • Add Microsoft.web.Administration dll referemce to the project library. you can find this library file in  system32\inetsrv folder.
  • Add the following line to your webconfig

         <identity impersonate="true" userName="adminname" password="adminpwd"/>

 

Create an Object with the name of IIS Object

public class IISObject

   {

   public long siteId { get; set; }

   public string hostName { get; set; }

   public string webSiteName { get; set; }

   public string portNumber { get; set; }

   public string applicationPool { get; set; }      

    }

 

Below is the code to add website binding to a Host 

 public void AddBindings(string sitename,string hostname)

   {    

   ServerManager serverMgr = new ServerManager();

   Site mySite = serverMgr.Sites[sitename];

   mySite.Bindings.Add("*:80:"+hostname, "http");

    // Set auto start to true.

   mySite.ServerAutoStart = true;

   // Commit the changes

   serverMgr.CommitChanges();

 }

Below is the code to add website to IIS

void AddWebSite( IISObject iisObject)

 {  

   ServerManager serverMgr = new ServerManager();                     

   string bindinginfo = "*:" + ConfigurationManager.AppSettings["portnumber"] + ":" + iisObject.hostName;

    //check if website name already exists in IIS

   Boolean bWebsite = IsWebsiteExists(iisObject.webSiteName, serverMgr);

    if (!bWebsite)

  {

   //ConfigurationManager.AppSettings["websitefolder"]  is the physical path of the directory

   Site mySite = serverMgr.Sites.Add(iisObject.webSiteName, "http", bindinginfo,ConfigurationManager.AppSettings["websitefolder"]);

   // name of the application pool, normally can use DefaultAppPool

   mySite.ApplicationDefaults.ApplicationPoolName =ConfigurationManager.AppSettings["applicationpool"];

 mySite.TraceFailedRequestsLogging.Enabled = true;

  string logginDirectory = ConfigurationManager.AppSettings["loggingdirectory"];

   mySite.TraceFailedRequestsLogging.Directory = logginDirectory + "\\" + iisObject.webSiteName;

   serverMgr.CommitChanges();

   } 

  }

Remove Binding from the Host Name

public  void RemoveServerBinding(string hostname,string sitename)

  {

  ServerManager serverMgr = new ServerManager();

    Site mySite = serverMgr.Sites[sitename];

   for (int i = 0; i < mySite.Bindings.Count; i++)

   {              

    if (mySite.Bindings[i].Host == hostname)

   {

    mySite.Bindings.RemoveAt(i);

   break;

  }

 } 

   mySite.ServerAutoStart = true

   }

Remove Website from IIS

public void RemoveIISWebsite(string sitename)

   { 

   ServerManager serverMgr = new ServerManager();

   Site site = serverMgr.Sites[sitename]; // you can pass the site name or the site ID

   //check site exists or not

   bool blnSuccess = false;

   blnSuccess = IsWebsiteExists(sitename, serverMgr);

   if (blnSuccess)

   {

   serverMgr.Sites.Remove(site);

   serverMgr.CommitChanges();              

   }          

   }

 

//Get all the website name and IDs

 

 public List<IISObject> GetIISSite()

  {

  //created an instance for pet object

  List<IISObject> iisSiteList = new List<IISObject>();

  ServerManager serverMgr = new ServerManager();

  SiteCollection sitecollection = serverMgr.Sites;

  foreach (var site in sitecollection)

  {

  IISObject issObject = new IISObject() { webSiteName = site.Name,siteId = site.Id };

  iisSiteList.Add(issObject);

  }

 

  return iisSiteList;

  }