IIS Create Site in asp.net

From: Chris Hood (Tech_at_computertuter.com)
Date: 02/26/04


Date: Thu, 26 Feb 2004 13:32:23 -0500

This is a copy of my class file....

Imports System.Collections

imports System.Web.HttpUtility

Imports System.Object

Imports System.Data

Imports System.Data.SqlClient

Imports System.Data.SqlTypes

Imports System.Management

Imports System

Imports System.ComponentModel

Imports System.Drawing

Imports System.Web

Imports System.Web.SessionState

Imports System.Web.UI

Public Class IISWMI

Inherits System.Web.UI.UserControl

Public Class SiteCreator

Shared Sub Main()

Dim rootVirtualDir As String = "c:\NewSite"

Dim site As String = "MySpecialSite"

Dim oConn As New ConnectionOptions()

'oConn.Username = "webAdministrator"

'oConn.Password = "adminpwd"

Dim myPath As New ManagementPath()

myPath.Server = "N"

myPath.NamespacePath = "root\MicrosoftIISv2"

myPath.RelativePath = "IIsWebService.Name='W3SVC'"

Dim scope As New ManagementScope(myPath, oConn)

Dim nac As New ManagementObject(scope, myPath, Nothing)

Try

' create a ServerBinding WMI object

Dim Bindings As ManagementObject = CreateInstance(oConn)

Bindings("IP") = ""

Bindings("Hostname") = ""

Bindings("Port") = "8083"

Bindings.Put() 'to commit the new instance.

Dim ServerBindings(1) As ManagementBaseObject

ServerBindings(0) = Bindings

Dim ret As String = CreateNewSite(nac, rootVirtualDir, ServerBindings, site)

Console.WriteLine("Return: {0} ", ret)

Finally

nac.Dispose()

End Try ' continue to configure and eventually manage the web server (start,
stop

End Sub 'Main

' Create bindings object in WMI repository

Public Shared Function CreateInstance(ByVal oConn As ConnectionOptions) As
ManagementObject

Dim mScope As ManagementScope = Nothing

Dim mPath As New ManagementPath()

mPath.ClassName = "ServerBinding"

mPath.NamespacePath = "root\MicrosoftIISv2"

mScope = New ManagementScope(mPath, oConn)

mScope.Path.Server = "."

Return New ManagementClass(mScope, mPath, Nothing).CreateInstance()

End Function 'CreateInstance

' Create new site

Public Shared Function CreateNewSite(ByVal nac As ManagementObject, ByVal
PathOfRootVirtualDir As String, ByVal ServerBindings() As
ManagementBaseObject, ByVal ServerComment As String) As String

', int ServerId

Dim inP As ManagementBaseObject = Nothing

inP = nac.GetMethodParameters("CreateNewSite")

inP("PathOfRootVirtualDir") = PathOfRootVirtualDir

inP("ServerBindings") = ServerBindings

inP("ServerComment") = ServerComment

' inP["ServerId"] = ServerId; // Let the system create an ID (calculated
hash of ManagementBaseObject outP = nac.InvokeMethod("CreateNewSite", inP,
null);

Return System.Convert.ToString(inP.Properties("ReturnValue").Value)

End Function 'CreateNewSite ' Returns something like:
IIsWebServer='W3SVC/1273343373'

End Class 'SiteCreator ' this can be used to further configure/customize the
webserver

Server Error in '/' Application.
----------------------------------------------------------------------------

----
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
      An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
   System.Management.PropertyData.MapValueToWmiValue(Object val, CimType
type, Boolean isArray) +1505
   System.Management.PropertyData.set_Value(Object value) +57
   System.Management.ManagementBaseObject.SetPropertyValue(String
propertyName, Object propertyValue) +96
   System.Management.ManagementBaseObject.set_Item(String propertyName,
Object value) +44
[ManagementException: Object reference not set to an instance of an object.]
   System.Management.ManagementException.ThrowWithExtendedInfo(Exception e)
+167
   System.Management.ManagementBaseObject.set_Item(String propertyName,
Object value) +54
   FormAuthentication.SiteCreator.CreateNewSite(ManagementObject nac, String
PathOfRootVirtualDir, ManagementBaseObject[] ServerBindings, String
ServerComment) in C:\Documents and
Settings\Administrator.INTERNALW\VSWebCache\cpanel.computertuter.com\membera
rea\Comp\IISWMI.vb:206
   FormAuthentication.SiteCreator.Main() in C:\Documents and
Settings\Administrator.INTERNALW\VSWebCache\cpanel.computertuter.com\membera
rea\Comp\IISWMI.vb:182
   FormAuthentication.iisbasic.Linkbutton4_Click(Object sender, EventArgs e)
in C:\Documents and
Settings\Administrator.INTERNALW\VSWebCache\cpanel.computertuter.com\membera
rea\Comp\iisbasic.ascx.vb:185
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +108
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.Rai
sePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
   System.Web.UI.Page.ProcessRequestMain() +1277
This is the error i am getting.  The lines the error refrences are off
beacuse i shorten.
the line it is refrencince is this one
inP("ServerBindings") = ServerBindings
Someone please help.  I am lost.  Been trying all kinda things to make it
work and i cant.