tags:

views:

607

answers:

3

I need to programatically create an IIS website. Can anybody show me the code to do this?

+3  A: 

This will work for IIS 6.0 and later, it's written in VB.Net (which, this is small enough to easily to convert to C# if needed). I also didn't write this, I found it here (I did compile it though to make sure it would build): http://www.gafvert.info/notes/VBNET-Create-Website-IIS6.htm

    Imports System.DirectoryServices
Imports System

Public Class IISAdmin
    Public Shared Function CreateWebsite(webserver As String, serverComment As String, serverBindings As String, homeDirectory As String) As Integer
        Dim w3svc As DirectoryEntry
        w3svc = New DirectoryEntry("IIS://localhost/w3svc")

        'Create a website object array
        Dim newsite() As Object
        newsite = New Object(){serverComment, new Object(){serverBindings}, homeDirectory}

        'invoke IIsWebService.CreateNewSite
        Dim websiteId As Object
        websiteId = w3svc.Invoke("CreateNewSite", newsite)

        Return websiteId

    End Function

    Public Shared Sub Main(args As String())
        Dim a As Integer
        a = CreateWebsite("localhost", "Testing.com", ":80:Testing.com", "C:\\inetpub\\wwwroot")
        Console.WriteLine("Created website with ID: " & a)
    End Sub

End Class
John
You beat me to it :)
Kirtan
I also suspect this will need to be run from the web-server because it points to IIS://localhost Not sure how it would work remotely, it would depend on your security setup.
John
@ Kirtan. Hehe. :) I usually don't post here, was a fluke, I saw the question and thought, I know I've seen that done somewhere, hehe.
John
C# equivalent to the abovehttp://www.gafvert.info/notes/CreateWebSiteIIS6.htm
MattH
A: 

You can do something like this. This shows how to create an IIS 6 website using VB.NET code.

Kirtan
A: 

The best place for information on IIS 7, that I found so far is here

CheGueVerra