Programmatically creating an IIS7 site

I’m using FinalBuilder to build and deploy websites on our test server. FinalBuilder is a great product, but our client is using Windows Server 2008 and FinalBuilder does not have actions for IIS7. I might add the actions to my weblog, but here’s some code to create a new IIS7 website using C#.

Create a new solution and add a reference to C:WindowsSystem32InetServMicrosoft.Web.Administration.dll. Paste the following code and you’ve got yourself a website on port 82 using the default application pool. In IIS it’s named MyCoolWebsite.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Web.Administration;


namespace TestApp


  class Program


    static void Main(string[] args)


      string siteName = “MyCoolWebsite”;

      string applicationPoolName = “DefaultAppPool”;

      string virtualDirectoryPath = “/”;

      string virtualDirectoryPhysicalPath = “C:\temp\”;

      string ipAddress = “*”;

      string tcpPort = “81”;

      string hostHeader = “”;

      string applicationPath = “/”;

      long highestId = 1;


      using (ServerManager mgr = new ServerManager())


        Site site = mgr.Sites[“siteName”];

        if (site != null)

          return; // Site bestaat al


        ApplicationPool appPool = mgr.ApplicationPools[applicationPoolName];

        if (appPool == null)

          throw new Exception(String.Format(“Application Pool: {0} does not exist.”, applicationPoolName));


        foreach (Site mysite in mgr.Sites)


          if (mysite.Id > highestId)

            highestId = mysite.Id;




        site = mgr.Sites.CreateElement();

        site.SetAttributeValue(“name”, siteName);

        site.Id = highestId;



        string bind = ipAddress + “:” + tcpPort + “:” + hostHeader;


        Binding binding = site.Bindings.CreateElement();

        binding.Protocol = “http”;

        binding.BindingInformation = bind;


        //site.Bindings.Add(bind, “http”);


        Application app = site.Applications.CreateElement();

        app.Path = applicationPath;

        app.ApplicationPoolName = applicationPoolName;

        VirtualDirectory vdir = app.VirtualDirectories.CreateElement();

        vdir.Path = virtualDirectoryPath;

        vdir.PhysicalPath = virtualDirectoryPhysicalPath;










You may also like...

6 Responses

  1. Ryan says:

    Great example! This is just what I needed to get started with IIS 7.

  2. Faheem Sial says:

    hi m using this code and i m getting Invalid Application PAth error.i m passing it following value;

    app.Path = “c:inetpubwwwrootmydomainwww”;

    could u please help me out and also tell me the important list of properties to set or list of all website properties.Thanx

  3. Yaron Avital says:

    HI, great example

    a minor typo,
    replace this line:
    Site site = mgr.Sites[“siteName”];

    with this:

    Site site = mgr.Sites[siteName];

  4. Dormand says:

    Nice. It works

  5. Geoff says:

    Thanks! Any idea how to specify a specific user/password for the connect as property?

  6. zeeshan says:

    Great Job man!!

Leave a Reply

Your email address will not be published. Required fields are marked *