Dennis van der Stelt

If you have one problem and use cache to solve it, you now have two problems.

Community

Email Notifications

News

  • Addicted to Refactor! Pro

I read...

I Use...

Tags

Recent Posts

Archives

Blog Subscription Form

  • Email Notifications
    Go

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:\Windows\System32\InetServ\Microsoft.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;

        }

        highestId++;

 

        site = mgr.Sites.CreateElement();

        site.SetAttributeValue("name", siteName);

        site.Id = highestId;

        site.Bindings.Clear();

 

        string bind = ipAddress + ":" + tcpPort + ":" + hostHeader;

 

        Binding binding = site.Bindings.CreateElement();

        binding.Protocol = "http";

        binding.BindingInformation = bind;

        site.Bindings.Add(binding);

        //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;

        app.VirtualDirectories.Add(vdir);

        site.Applications.Add(app);

 

        mgr.Sites.Add(site);

        mgr.CommitChanges();

      }

    }

  }

}

Comments

Ryan said:

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

# July 21, 2008 8:00 PM

Faheem Sial said:

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

app.Path = "c:\inetpub\wwwroot\mydomain\www";

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

# November 14, 2008 2:48 PM

Yaron Avital said:

HI, great example

a minor typo,

replace this line:

Site site = mgr.Sites["siteName"];

with this:

Site site = mgr.Sites[siteName];

# August 13, 2009 3:02 PM

Dormand said:

Nice. It works

# March 8, 2010 12:03 PM

Jordan shoes said:

We just couldnt leave your website before saying that we really enjoyed the quality information you offer to your visitors... Will be back often to check up on new stuff you post!

# September 10, 2010 10:42 AM

Geoff said:

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

# October 22, 2010 2:20 PM

zeeshan said:

Great Job man!!

# June 13, 2011 11:27 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 1 and 8 and type the answer here: