?
Solved

VB.NET Create Virtual Directory

Posted on 2008-10-17
8
Medium Priority
?
799 Views
Last Modified: 2012-05-05
I am using two scripts that I found that I can create a Virtual Dirctory or Web Site.  I have two questions:

CreateVirtualDir:  When I run this, it creats the virtual directory fine, but it creates it in the Default Web site.  I'd like to specify a Web site for the VD to be created.  How?

CreateWebsite:  This creates a web site with no problems, however it does not automatically start.  How can I invoke start?  Lastly, how do I get this to first check if the web site exists and if so not add the site again?
Sub CreateVirtualDir(ByVal WebSite As String, ByVal AppName As String, ByVal Path As String)
 
        Dim IISSchema As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/Schema/AppIsolated")
        Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
        IISSchema.Dispose()
 
        If CanCreate Then
            Dim PathCreated As Boolean
 
            Try
                Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/1/Root")
 
                'make sure folder exists
                If Not System.IO.Directory.Exists(Path) Then
                    System.IO.Directory.CreateDirectory(Path)
                    PathCreated = True
                End If
 
                'If the virtual directory already exists then delete it
                For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
                    If VD.Name = AppName Then
                        IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
                        IISAdmin.CommitChanges()
                        Exit For
                    End If
                Next VD
          'Create and setup new virtual directory
                    Dim VDir As System.DirectoryServices.DirectoryEntry = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir")
                    VDir.Properties("Path").Item(0) = Path
                    VDir.Properties("AppFriendlyName").Item(0) = AppName
                    VDir.Properties("EnableDirBrowsing").Item(0) = False
                    VDir.Properties("AccessRead").Item(0) = True
                    VDir.Properties("AccessExecute").Item(0) = True
                    VDir.Properties("AccessWrite").Item(0) = True
                    VDir.Properties("AccessScript").Item(0) = True
                    VDir.Properties("AuthNTLM").Item(0) = True
                    VDir.Properties("EnableDefaultDoc").Item(0) = True
                    '  VDir.Properties("DefaultDoc").Item(0) = "Index.aspx"
                    VDir.Properties("AspEnableParentPaths").Item(0) = True
                    VDir.CommitChanges()
 
                    'the following are acceptable params
                    'INPROC = 0
                    'OUTPROC = 1
                    'POOLED = 2
                    VDir.Invoke("AppCreate", 1)
                End If
            Catch Ex As Exception
                If PathCreated Then
                    System.IO.Directory.Delete(Path)
                End If
                Throw Ex
            End Try        
    End Sub
    Public Function CreateWebsite(ByVal webserver As String, ByVal serverComment As String, ByVal serverBindings As String, ByVal 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)
           
''''  My Attempt to get the start to work automatically...doesn't work.
        'newsite = w3svc.Invoke("Start", System.DBNull.Value)
 
        Return websiteId
    End Function

Open in new window

0
Comment
Question by:azyet24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 22749059
>CreateWebsite: This creates a web site with no problems, however it does not automatically start. How can I invoke start?

When you create your website, you need to keep track of the websiteID variable that is returned from the invoke. That is the reference you will use for getting a reference to the directoryservices object needed for the invoke:

Dim NewSite As New System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/" & websiteID.ToString)
If Not NewSite Is Nothing Then
NewSite.Invoke("Start", Nothing)
End If

>I'd like to specify a Web site for the VD to be created. How?

Here again, you need to use the websiteID returned above. In this line of the CreateVirtualDir sub, you need to specify the correct ID instead of 1 which is the default web site:

Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/" & websiteID.ToString & "/Root")


>Lastly, how do I get this to first check if the web site exists and if so not add the site again?

Since you don't know the ID of the site at this time, only the name, you have to loop through all the sites, and check if the name exists.




    Private Function CheckWebsiteExists(ByVal Name As String) As Boolean
        Using websvc As New System.DirectoryServices.DirectoryEntry("IIS://" & Environment.MachineName & "/W3svc")
            
            For Each site As System.DirectoryServices.DirectoryEntry In websvc.Children
                If site.SchemaClassName = "IIsWebServer" Then
                    Debug.WriteLine(site.Name) 'This is the ID if you want it.
                    If Name = site.InvokeGet("ServerComment").ToString Then
                        Return True
                    End If
                End If
            Next
        End Using
        Return False
    End Function

Open in new window

0
 

Author Comment

by:azyet24
ID: 22749259
>I'd like to specify a Web site for the VD to be created. How?

>Here again, you need to use the websiteID returned above. In this line of the CreateVirtualDir sub, you >need to specify the correct ID instead of 1 which is the default web site:

>Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/" & >websiteID.ToString & "/Root")

This is updating the correct website, but it creates application rather than virtualhost (icon is glob instead of folder).  When I right click on the web site, there is two options: Add Application, Add Virtual Host.  I want the virtual host.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 22750257
Maybe you should post the full source for which you are having the problem.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:azyet24
ID: 22750666

CreateVirtualDir("LocalHost", name, "C:\Inetpub\wwwroot\Communities")
 
 
    Sub CreateVirtualDir(ByVal WebSite As String, ByVal AppName As String, ByVal Path As String)
        Dim servername As String
        servername = "XPSM1210"
        Dim root As New System.DirectoryServices.DirectoryEntry("IIS://" & servername & "/W3SVC")
        Dim IISSchema As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/Schema/AppIsolated")
        Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
        IISSchema.Dispose()
        If CanCreate Then
            Dim PathCreated As Boolean
 
            Try
                Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/" & 2 & "/Root")
 
                'make sure folder exists
                If Not System.IO.Directory.Exists(Path) Then
                    System.IO.Directory.CreateDirectory(Path)
                    PathCreated = True
                End If
 
                'If the virtual directory already exists then delete it
                For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
                    If VD.Name = AppName Then
                        IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
                        IISAdmin.CommitChanges()
                        Exit For
                    End If
                Next VD
                Dim avoid As Integer = 1
                If avoid = 1 Then
                    'Create and setup new virtual directory
                    Dim VDir As System.DirectoryServices.DirectoryEntry = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir") '(VDirName, "IIsWebVirtualDir")
                    VDir.Properties("Path").Item(0) = Path
                    VDir.Properties("AppFriendlyName").Item(0) = AppName
                    VDir.Properties("EnableDirBrowsing").Item(0) = False
                    VDir.Properties("AccessRead").Item(0) = True
                    VDir.Properties("AccessExecute").Item(0) = True
                    VDir.Properties("AccessWrite").Item(0) = True
                    VDir.Properties("AccessScript").Item(0) = True
                    VDir.Properties("AuthNTLM").Item(0) = True
                    VDir.Properties("EnableDefaultDoc").Item(0) = True
                    '  VDir.Properties("DefaultDoc").Item(0) = "Index.aspx"
                    VDir.Properties("AspEnableParentPaths").Item(0) = True
                    VDir.CommitChanges()
 
                    'the following are acceptable params
                    'INPROC = 0
                    'OUTPROC = 1
                    'POOLED = 2
                    VDir.Invoke("AppCreate", 1)
                End If
            Catch Ex As Exception
                If PathCreated Then
                    System.IO.Directory.Delete(Path)
                End If
                Throw Ex
            End Try
        End If
    End Sub

Open in new window

0
 
LVL 38

Expert Comment

by:PaulHews
ID: 22753062

>This is updating the correct website, but it creates application rather than virtualhost (icon is glob instead of folder).  When I right click on the web site, there is two options: Add Application, Add Virtual Host.  I want the virtual host.<

Okay, I see what you mean now.  Comment out this line from the above:

VDir.Invoke("AppCreate", 1)

That's what takes your virtual directory and creates an application... I would leave it in if you are deploying an ASP.NET application however.



0
 

Author Comment

by:azyet24
ID: 22753726
Yes that worked perfectly.  Woulld you also know how to delete a website and/or virtual domain via vb.net?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 22759028
There's a bit in CreateVirtualDir above shows how to delete a virtual directory:

'If the virtual directory already exists then delete it
For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
    If VD.Name = AppName Then
        IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
        IISAdmin.CommitChanges()
        Exit For
    End If
Next VD

I'm not sure how to delete a web site, but I think I answered your original question pretty completely.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 22759149
Okay, this worked to delete a web site.  No error checking or anything, but should give the basic idea.
    Private Sub DeleteWebSite(ByVal ID As String)
        Using web As New System.DirectoryServices.DirectoryEntry("IIS://" & Environment.MachineName & "/W3svc/" & ID)
            web.Invoke("Stop")
            web.DeleteTree()
        End Using
    End Sub

Open in new window

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question