VB.NET - Create new website on IIS 6.0 by code
(Kristofer Gafvert, July 7, 2005)Web hosting companies needs to frequently create and delete websites. Some web hosters even provide their customers with forms to create websites themselves. Others have so called control panels so their customers can control and administrate their own website. For this purpose, it is important to be able to create websites by code.
The following example creates a new website by using Visual Basic .NET (VB.NET) and ADSI. The method IIsWebService.CreateNewSite is only available in IIS 6.0 and later, and using this method is the prefered way to create new websites by code in IIS 6.0. This example does not specify the identification number, but if you want to do so, you can (see the reference for the method at the bottom of this page). After the website has been created, it will be stopped. The website can be started either manually, or by code (invoke the Start method).
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
To compile using the command line compiler (vbc.exe), type:
vbc /r:system.directoryservices.dll /r:system.dll CreateWebsiteIIS6.vb
Resources
Download code (VBNETCreateWebsiteIIS6.zip)MSDN: IIsWebService.CreateNewSite
C# Example of creating a new website on IIS 6.0