Create an IIS 6 Website entry programmatically in .NET

With IIS7 there is an easy API to work with IIS entries. But if you still have a server running on IIS6 and manage allot of virtual sites then it does come to a point where manually adding the sites becomes. .. tedious.

Many examples demonstrate how to create the IIS entries and a new Virtual Directory. In my case I already have directories created, using another automated process for a staging domain that points to the root folder containing all my sites.

So, I just want to add a new IIS entry with the correct permissions and accept domain connections on the specific name directly to the root of an existing directory.

For reference it is good to look at this page. But it was still pretty difficult to understand exactly how to set properties and invoke actions. End of the day.. or two... a few lines of code does the trick :-)

    ''' Create a New Site in IIS
    ''' "domainName">Domain name and domain without http or www. ie ''
    ''' "homeDirectory">Direcotry on server of containing folder
    ''' An IIS id for reference
    Public Shared Function CreateWebsite(ByVal domainName As StringByVal homeDirectory As StringAs String
        Dim serverBindings As Object() = New Object(1) {}
        serverBindings(0) = ":80:" & domainName
        serverBindings(1) = ":80:" & "www." & domainName
        Dim IISDomainName As String = Char.ToUpper(domainName(0)) + domainName.Split(CChar("."))(0).Substring(1) & "SuperSite"
        'I have imnpersonate setup in my web.config - Administrator rights are requried.
        Dim w3svc As New DirectoryEntry("IIS://localhost/W3SVC")
            'Create new Site in IIS with default configuration
            Dim newSite As Object() = New Object() {IISDomainName, serverBindings, homeDirectory}
            Dim newSiteId As Object = w3svc.Invoke("CreateNewSite", newSite)

            'Use the new sites ID and configure as neede
            'Impersonate does not seem to apply here? So just supply password again for admin account.
            w3svc = New DirectoryEntry("IIS://localhost/W3SVC/" & newSiteId.ToString, "username""password!")
            w3svc.Properties("EnableDefaultDoc")(0) = True
            w3svc.Properties("DefaultDoc").Value = "home.aspx,adminhome.aspx"
            w3svc.Properties("AppFriendlyName")(0) = "MySite"
            w3svc.Properties("AppIsolated")(0) = 2
            w3svc.Properties("AccessRead")(0) = True
            w3svc.Properties("AccessWrite")(0) = False
            w3svc.Properties("AccessScript")(0) = True
            w3svc.Properties("AccessFlags").Value = 513
            w3svc.Properties("AppPoolId").Value = "mySitePoolName"
            w3svc.Properties("AuthNTLM")(0) = True
            w3svc.Properties("AuthAnonymous")(0) = True
            w3svc.Properties("ServerAutoStart")(0) = True


            'Start the site
        Catch ex As Exception
            If ex.ToString.Contains("0x800700B7"Then
                Return "Website already exists."
                Throw New Exception("Website already exists!")
                Return ex.ToString
                Throw New Exception("Exception:" & vbNewLine & ex.ToString)
            End If
        End Try
        Return w3svc.Name
    End Function