Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Creating virtual directory using .NET Framework.

Author: DevASP
Download Source Code :

This article is about how you can create a virtual directory using .NET framework.

Using Internet Information Services (IIS) Manager, you can create a virtual directory for an ASP.NET Web application. A virtual directory appears to client browsers as though it were contained in a Web server's root directory, even though it can physically reside somewhere else. This approach enables you to publish Web content that is not located under the root folder on your Web server, such as content that is located on a remote computer. It is also a convenient way to set up a site for local Web development work because it does not require a unique site identity, which means that it requires fewer steps than creating a unique site. This topic explains how you can create a virtual directory using VS.NET to run ASP.NET pages.

To create a virtual directory, a Web site must already be created on the Web server. IIS creates a default Web site on the computer during installation. If you have not created a site of your own, you can create a virtual directory under the default Web site.


Steps you will do. 

Start visual studio and create a new widow application for 

Set the following properties of the form object.


Name = “frmMain”

Text = “Create Virtual Directory”

MaximizeBox = “False”


Drop two label controls on the form and set their following properties. First label control.


Name = “Label1”

Text = “Enter the virtuall directory name.”


Second label control.


Name = “Label2”

Text = “Enter the virtuall directory path. e.g c:\devasp\one”


Now drop two text fields on the form and set their following properties.

First text box control


Name = “txtDrictoryName”

Text = “”


Second text box control


Name = “txtDrictoryPath”

Text = “”



Drop a button control on the form and set its following properties.


Name = “btnCreate”

Text = “Create”

Open the code window of form and import the following namespace.


Imports System.DirectoryServices


Declare a function as below to create the virtual directory.


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"




If CanCreate Then

      Dim PathCreated As Boolean



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


'make sure folder exists

If Not System.IO.Directory.Exists(Path) Then


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})



Exit For


End If


Next VD


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.Invoke("AppCreate", 1)


Catch Ex As Exception


    If PathCreated Then


    End If


Throw Ex

End Try


End If


Write the following code in the click event of the create button to call the above function and pass the parameters as below.


CreateVirtualDir("LocalHost", txtDrictoryName.Text, txtDrictoryPath.Text)


MsgBox("Created Successfully")


Run your application. Enter the name with which you want to create virtual directory in IIS. Enter the path where you want to create the virtual directory on hard drive and finally click the create button to create the virtual directory.

Article Comments
This is working only if i set the Application Pool Identity to Prefered - local System only. But unfortunately by default this Application Pool Identity is Network Service. and if i keep the default setting ( Means Network Service ) then i an unable to create virtual directory,.. Access Denied Exception is Thrown . Please help me

Posted on 12/6/2006 12:08:31 AM by Nilesh

i am trying to use code for remote server but its give me the same error can you please tell me how to set for remote server

Posted on 1/24/2007 1:24:05 AM by Devang

i haven't tried this code exactly, but it looks similar to what i have done. Works fine on other servers too. check for any firewalls that exist as that is usually the cause

Posted on 10/29/2007 11:48:28 PM by Kevin Storey

Can anyone explain C# code to create a virtual folde. actually my purpose is to create a folder dynamically and point it through virtual directory so that it may be acces through the URL of the folder.

Posted on 2/18/2008 2:13:51 AM by Vibhu

Add Article Comment:
Name :
Email Address :
Comments :
<< Using Image characters verification to register a user on a web site.

Disclaimer - Privacy
© 2002-2017