Solved

Create Virtual Directory in my Server

Posted on 2006-06-29
10
334 Views
Last Modified: 2013-11-15
Hi All!
I want to create Virtual Directory in my server by ASP.NET. Help me !
Example: I type name of Virtural Directory, path and then I click "Create VT" button, Virtual Directory created with that name.
Help me, thanks
0
Comment
Question by:duynnh
  • 5
  • 2
10 Comments
 
LVL 19

Expert Comment

by:DreamMaster
ID: 17010094
.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) = False
                VDir.Properties("AccessScript").Item(0) = True
                VDir.Properties("AuthNTLM").Item(0) = True
                VDir.Properties("EnableDefaultDoc").Item(0) = True
                VDir.Properties("DefaultDoc").Item(0) = "default.htm,default.aspx,default.asp"
                VDir.Properties("AspEnableParentPaths").Item(0) = True
                VDir.CommitChanges()

                'the following are acceptable params
                'INPROC = 0
                'OUTPROC = 1
                'POOLED = 2
                VDir.Invoke("AppCreate", 1)

            Catch Ex As Exception
                If PathCreated Then
                    System.IO.Directory.Delete(Path)
                End If
                Throw Ex
            End Try
        End If
    End Sub
--------------------------------------------------------------------------------
This is used in the following way:

visual basic code:--------------------------------------------------------------------------------
CreateVirtualDir("LocalHost", "Woof", "C:\MyWebProjects\Woof")
--------------------------------------------------------------------------------
This creates a Virtual Dir called Woof on the LocalHost server and points and the path C:\MyWebProjects\Woof on the hard drive.

So you could easily get the name from a form.

Hope this works. It does for me!

Regards,
Max.
0
 
LVL 19

Accepted Solution

by:
DreamMaster earned 250 total points
ID: 17010104
Hmm...the code didn't fully copy...the sub should look like this:

    Private 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) = False
                VDir.Properties("AccessScript").Item(0) = True
                VDir.Properties("AuthNTLM").Item(0) = True
                VDir.Properties("EnableDefaultDoc").Item(0) = True
                VDir.Properties("DefaultDoc").Item(0) = "default.htm,default.aspx,default.asp"
                VDir.Properties("AspEnableParentPaths").Item(0) = True
                VDir.CommitChanges()

                'the following are acceptable params
                'INPROC = 0
                'OUTPROC = 1
                'POOLED = 2
                VDir.Invoke("AppCreate", 1)

            Catch Ex As Exception
                If PathCreated Then
                    System.IO.Directory.Delete(Path)
                End If
                Throw Ex
            End Try
        End If
    End Sub


Dont forget, You need to add a reference into your app for System.DirectoryServices

Regards,
Max.
0
 

Author Comment

by:duynnh
ID: 17010167
Hi  DreamMaster!
This Error:
Type System.DirectoryServices.DirectoryEntry is not defined.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:duynnh
ID: 17010266
Hi Max!
I have ran code from you and this is Error:

"[UnauthorizedAccessException: Access is denied.]
   vungoimora.WebForm3.CreateVirtualDir(String WebSite, String AppName, String Path) in K:\My Documents\MyProjects\Vungoimora\MainProject\vungoimora\WebForm3.aspx.vb:139
   vungoimora.WebForm3.Button2_Click(Object sender, EventArgs e) in K:\My Documents\MyProjects\Vungoimora\MainProject\vungoimora\WebForm3.aspx.vb:83
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()
"
Can you give me the solution to solve this problem. Thanks
Regards,
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 17015965
You will need to have rights to write in your folder offcourse. It says it right there in your error.

Probably you don't have the correct security rights to write into the folder you are in...

Regards,
Max.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 17168417
Well, I provided a working solution, and gave enough information to solve the issue.

Regards,
Max.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 17236449
Thank you DarthMod :)

Regards,
Max.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
VM backup deduplication is a method of reducing the amount of storage space needed to save VM backups. In most organizations, VMs contain many duplicate copies of data, such as VMs deployed from the same template, VMs with the same OS, or VMs that h…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

832 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