Solved

Create Virtual Directory in my Server

Posted on 2006-06-29
10
314 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
Comment Utility
.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
Comment Utility
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
Comment Utility
Hi  DreamMaster!
This Error:
Type System.DirectoryServices.DirectoryEntry is not defined.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:duynnh
Comment Utility
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
Comment Utility
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
Comment Utility
Well, I provided a working solution, and gave enough information to solve the issue.

Regards,
Max.
0
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
Thank you DarthMod :)

Regards,
Max.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now