I changed the SiteCollection.Add call to pass in an empty string for physical path and zero for port number. But this did create a problem - the SiteCollection.Add method requires the physical path and port number. I wanted to keep the parameters passed to AddSite to a minimum. Notice that I no longer pass in the physical path and port number to the AddSite method. Var site = sm.Sites.Add(siteName, "", 0) Var appPool = sm.ApplicationPools.Add(poolName) Var siteNode = (string.Format("Site: ", siteName)) Using (var serverManager = new ServerManager())įoreach (var site in serverManager.Sites) Listing 1: Form_Load Event Showing Information for Local IIS Server in a Tree View It displays basic information for the local IIS server in a tree view, as you can see in Listing 1.
Then I dropped a few lines of code into the Form_Load event.
To show you what's possible, I created a new Windows Forms application and used NuGet to install the package. The sample code included in this article contains a complete example of programmatically creating an IIS site, complete with virtual directories, application pools and security settings. Likewise, if you're going to do debugging of your utilities from inside Visual Studio, you'll need to be running Visual Studio with Administrator access. Therefore, any utilities you write using the techniques covered in this article must be executed by an administrator.
I decided to put my programming skills to work and automate this task.Īn important point I'd like to begin with: Access to IIS Manager is restricted to Administrators. Either way, when it comes to setting up IIS on my new environment, I grew tired of the myriad of settings and options that had to be configured for all these sites. It's either a new version of Windows or, most recently, a solid-state drive (SSD) that died on me. These sites are all hosted by Web hosting companies, but I have a copy of all of them set up on my development machine for maintenance and testing.įrom time to time, I have to do a clean install of my machine. Nothing major - a church Web site, a small PHP Web site for the salon my wife works at and a hot-lunch-ordering Web site for my kid's school.
I maintain a number of Web sites outside my day job.