Virtual Directory <virtualDirectory>


The <virtualDirectory> element is a child of the <application> element and controls the configuration settings for a specific virtual directory. A virtual directory is a directory name (also referred to as path) that you specify in Internet Information Services (IIS) 7 and map to a physical directory on a local or remote server. The virtual directory name becomes part of the application's URL, and users can request the URL from a browser to access content in the physical directory, such as a Web page or a list of additional directories and files. If you specify a different name than the physical directory for the virtual directory, it is more difficult for users to discover the actual physical file structure on your server because the URL does not map directly to the root of the site.

In IIS 7, each application must have a virtual directory, known as the root virtual directory, and maps the application to the physical directory that contains the application's content. However, an application can have more than one virtual directory. For example, you might use a virtual directory when you want your application to include images from another location in the file system, but you do not want to move the image files into the physical directory that is mapped to the application's root virtual directory.


Version Notes
IIS 8.5 The <virtualDirectory> element was not modified in IIS 8.5.
IIS 8.0 The <virtualDirectory> element was not modified in IIS 8.0.
IIS 7.5 The <virtualDirectory> element was not modified in IIS 7.5.
IIS 7.0 The <virtualDirectory> element was introduced in IIS 7.0.
IIS 6.0 The <virtualDirectory> element replaces the IIS 6.0 IIsWebVirtualDir and IIsWebDirectory metabase objects.


The <virtualDirectory> element is included in the default installation of IIS 7.

How To

How to create a new Virtual Directory

  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2012 or Windows Server 2012 R2:
      • On the taskbar, click Server Manager, click Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows 8 or Windows 8.1:
      • Hold down the Windows key, press the letter X, and then click Control Panel.
      • Click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, expand the server name, expand Sites, expand the Web site to which you want to add the virtual directory, and then click the application to which you want to add the virtual directory.

  3. In the Actions pane, click View Virtual Directories, and then click Add Virtual Directory...
  4. In the Add Virtual Directory dialog box, at a minimum enter information in the Alias: and Physical path: text boxes, and then click OK.

    Note: You can click Connect as... to configure the server to use the specified credentials when it accesses content in the virtual directory, and click Test Settings... to test authentication and authorization settings for the virtual directory.


The <virtualDirectory> element is configurable at the server, site, and application level in the ApplicationHost.config file.


Attribute Description
allowSubDirConfig Optional Boolean attribute.

Specifies whether IIS looks for Web.config files in content directories lower than the current level (true) or does not look for Web.config files in content directories lower than the current level (false).

The default value is true.
logonMethod Optional enum attribute.

Specifies the logon method for the virtual directory.

The logonMethod attribute can be one of the following possible values. The default is ClearText.
Value Description
Batch This logon type is intended for batch servers, where processes may be executing on behalf of a user without the user's direct intervention.

The numeric value is 1.
ClearText This logon type preserves the name and password in the authentication package, which allows the server to make connections to other network servers while impersonating the client.

The numeric value is 3.
Interactive This logon type is intended for users who will be using the computer interactively.

The numeric value is 0.
Network This logon type is intended for high performance servers that authenticate plaintext passwords. Credentials are not cached for this logon type.

The numeric value is 2.
password Optional string attribute.

Specifies the password associated with the user name.

Note: To avoid storing unencrypted password strings in configuration files, always use AppCmd.exe or IIS Manager to enter passwords. If you use these management tools, password strings will be encrypted automatically before they are written to the XML configuration files. This provides better password security than storing unencrypted passwords.
path Optional string attribute.

Specifies the virtual path of the virtual directory.
physicalPath Optional string attribute.

Specifies the physical path of the virtual directory.
userName Optional string attribute.

Specifies the user name of an account that can access configuration files and content for this virtual directory.

Child Elements


Configuration Sample

The following configuration example displays a <site> element that contains two applications. The first <application> element defines the root application for the site, along with the root virtual directory for the application. The second <application> element contains the configuration settings for a CRM application on the Web site. The path attribute defines the path for the application. This <application> element contains two <virtualDirectory> elements. The first defines the root virtual directory for the application, and the second defines an Images virtual directory for the application.

<site name="Contoso" id="2" serverAutoStart="true">
   <application path="/">
      <virtualDirectory path="/" physicalPath="C:\Contoso\Content" />
   <application path="/CRM">
      <virtualDirectory path="/" physicalPath="C:\Contoso\Content\CRM" />
      <virtualDirectory path="/Images" physicalPath="E:\Images" />
      <binding protocol="http" bindingInformation="*" />

Sample Code

The following examples create a new application named ShoppingCart with a virtual directory root of C:\Inetpub\Contoso\ShoppingCart for a site that is named Contoso.

Note: When you create an application from the command prompt or from a script, you must explicitly create a root virtual directory for the application.


appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].[path='/ShoppingCart']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].[path='/ShoppingCart'].[path='/',physicalPath='C:\Inetpub\Contoso\ShoppingCart']" /commit:apphost

Note: You must be sure to set the commit parameter to apphost when you use AppCmd.exe to configure these settings. This commits the configuration settings to the appropriate location section in the ApplicationHost.config file.


using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
   private static void Main()
      using (ServerManager serverManager = new ServerManager())
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
         ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
         ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Contoso");

         if (siteElement == null) throw new InvalidOperationException("Element not found!");

         ConfigurationElementCollection siteCollection = siteElement.GetCollection();
         ConfigurationElement applicationElement = siteCollection.CreateElement("application");

         applicationElement["path"] = @"/ShoppingCart";
         ConfigurationElementCollection applicationCollection = applicationElement.GetCollection();
         ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory");
         virtualDirectoryElement["path"] = @"/";
         virtualDirectoryElement["physicalPath"] = @"C:\Inetpub\Contoso\ShoppingCart";


   private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
      foreach (ConfigurationElement element in collection)
         if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
            bool matches = true;
            for (int i = 0; i < keyValues.Length; i += 2)
               object o = element.GetAttributeValue(keyValues[i]);
               string value = null;
               if (o != null)
                  value = o.ToString();
               if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
                  matches = false;
            if (matches)
               return element;
      return null;
   } }


Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "Contoso")

      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim siteCollection As ConfigurationElementCollection = siteElement.GetCollection
      Dim applicationElement As ConfigurationElement = siteCollection.CreateElement("application")
      applicationElement("path") = "/ShoppingCart"
      Dim applicationCollection As ConfigurationElementCollection = applicationElement.GetCollection
      Dim virtualDirectoryElement As ConfigurationElement = applicationCollection.CreateElement("virtualDirectory")
      virtualDirectoryElement("path") = "/"
      virtualDirectoryElement("physicalPath") = "C:\Inetpub\Contoso\ShoppingCart"

   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            If matches Then
               Return element
            End If
         End If
      Return Nothing
   End Function
End Module


var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;
var siteElementPos = FindElement(sitesCollection, "site", ["name", "Contoso"]);

if (siteElementPos == -1) throw "Element not found!";

var siteElement = sitesCollection.Item(siteElementPos);
var siteCollection = siteElement.Collection;
var applicationElement = siteCollection.CreateNewElement("application");
applicationElement.Properties.Item("path").Value = "/ShoppingCart";
var applicationCollection = applicationElement.Collection;
var virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory");
virtualDirectoryElement.Properties.Item("path").Value = "/";
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\\Inetpub\\Contoso\\ShoppingCart";


function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
         if (matches) {
            return i;
   return -1;


Set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "Contoso"))

If (siteElementPos = -1) Then
   WScript.Echo "Element not found!"
End If

Set siteElement = sitesCollection.Item(siteElementPos)
Set siteCollection = siteElement.Collection
Set applicationElement = siteCollection.CreateNewElement("application")
applicationElement.Properties.Item("path").Value = "/ShoppingCart"
Set applicationCollection = applicationElement.Collection
Set virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory")
virtualDirectoryElement.Properties.Item("path").Value = "/"
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\Inetpub\Contoso\ShoppingCart"


Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         If matches Then
            Exit For
         End If
      End If
   If matches Then
      FindElement = i
      FindElement = -1
   End If
End Function
Deprecated Elements