Link to home
Create AccountLog in
Avatar of talker2004
talker2004Flag for United States of America

asked on

Trouble Creating Virtual Directory in IIS in Vb.net C#

I have posted this question several times before but never really got a solution. I am having trouble with this code which is designed to create a vdir on my server. Works fine on the development machine. I have got everything from it creating the vdir under the wrong site to getting an rpc exception.

Anyway it works perfectly on my development machine which only has a single website responding only to localhost / ip address.

I have four websites on the server separated by host headers.

Could somebody show me how to use this code in order to create the following vdir under the appropriate website?

https://secure.mydomain.com/MYNEWVDIR

Thanks in advance.

Imports System
Imports System.IO
Imports System.DirectoryServices
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Collections
 
Module iisManager
 
    Sub Main(ByVal args() As String)
        CreateAppPool("IIS://Localhost/W3SVC/AppPools", "MyAppPool")
        CreateVDir("IIS://Localhost/W3SVC/1/Root", "MyVDir", "c:\Inetpub\Wwwroot")
        AssignVDirToAppPool("IIS://Localhost/W3SVC/1/Root/MyVDir", "MyAppPool")
    End Sub
 
 
 
    Public Sub CreateAppPool(ByVal strMetabasePath As String, ByVal strAppPoolName As String)
        ' strMetabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
        '   For example: "IIS://localhost/W3SVC/AppPools" 
        ' strAppPoolName is of the form "<name>", for example, "MyAppPool"
        Console.WriteLine(vbLf + "Creating application pool named {0}/{1}:", strMetabasePath, strAppPoolName)
 
        Try
            If strMetabasePath.EndsWith("/W3SVC/AppPools") Then
                Dim objNewPool As DirectoryEntry
                Dim objAppPools As New DirectoryEntry(strMetabasePath)
                objNewPool = objAppPools.Children.Add(strAppPoolName, "IIsApplicationPool")
                objNewPool.CommitChanges()
                Console.WriteLine("Done.")
            Else
                Console.WriteLine("Failed in CreateAppPool; application pools can only be created in the */W3SVC/AppPools node.")
            End If
        Catch exError As Exception
            Console.WriteLine("Failed in CreateAppPool with the following exception: " + vbLf + "{0}", exError.Message)
        End Try
 
    End Sub
 
 
    Public Sub CreateVDir(ByVal strMetabasePath As String, ByVal strVdirName As String, ByVal strPhysicalPath As String)
        ' strMetabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
        '   For example: "IIS://localhost/W3SVC/1/Root" 
        ' strVdirName is of the form "<name>", for example, "MyNewVDir"
        ' strPhysicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
        Console.WriteLine(vbLf + "Creating virtual directory {0}/{1}, mapping the Root application to {2}:", strMetabasePath, strVdirName, strPhysicalPath)
 
        Try
            Dim objSite As New DirectoryEntry(strMetabasePath)
            Dim strClassName As String = objSite.SchemaClassName.ToString()
            If strClassName.EndsWith("Server") OrElse strClassName.EndsWith("VirtualDir") Then
                Dim objVdirs As DirectoryEntries = objSite.Children
                Dim objNewVdir As DirectoryEntry = objVdirs.Add(strVdirName, strClassName.Replace("Service", "VirtualDir"))
                objNewVdir.Properties("Path")(0) = strPhysicalPath
                objNewVdir.Properties("AccessScript")(0) = True
                ' These properties are necessary for an application to be created.
                objNewVdir.Properties("AppFriendlyName")(0) = strVdirName
                objNewVdir.Properties("AppIsolated")(0) = "1"
                objNewVdir.Properties("AppRoot")(0) = "/LM" + strMetabasePath.Substring(strMetabasePath.IndexOf("/", "IIS://".Length))
                objNewVdir.CommitChanges()
                Console.WriteLine("Done.")
            Else
                Console.WriteLine("Failed. A virtual directory can only be created in a site or virtual directory node.")
            End If
        Catch exError As Exception
            Console.WriteLine("Failed in CreateVDir with the following exception: " + vbLf + "{0}", exError.Message)
        End Try
 
    End Sub
 
 
 
    Public Sub AssignVDirToAppPool(ByVal strMetabasePath As String, ByVal strAppPoolName As String)
        ' strMetabasePath is of the form "IIS://<servername>/W3SVC/<siteID>/Root[/<vDir>]"
        '   For example: "IIS://localhost/W3SVC/1/Root/MyVDir" 
        ' strAppPoolName is of the form "<name>", for example, "MyAppPool"
        Console.WriteLine(vbLf + "Assigning application {0} to the application pool named {1}:", strMetabasePath, strAppPoolName)
 
        Try
            Dim objVdir As New DirectoryEntry(strMetabasePath)
            Dim strClassName As String = objVdir.SchemaClassName.ToString()
            If strClassName.EndsWith("VirtualDir") Then
                Dim objParam As Object() = {0, strAppPoolName, True}
                objVdir.Invoke("AppCreate3", objParam)
                objVdir.Properties("AppIsolated")(0) = "2"
                Console.WriteLine("Done.")
            Else
                Console.WriteLine("Failed in AssignVDirToAppPool; only virtual directories can be assigned to application pools")
            End If
        Catch exError As Exception
            Console.WriteLine("Failed in AssignVDirToAppPool with the following exception: " + vbLf + "{0}", exError.Message)
        End Try
 
    End Sub
 
 
 
End Module

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of talker2004

ASKER

I have never been able to Create a vdir to a specific website.