In IIS 6 you could create a virtual directory with execute permissions, see code below, but running the code on IIS 7 would not set the AccessFalgs found at":
> start > run > inetmgr > servername > Sites > SiteName > Click on Handler Mappings > Edit Feature Permissions >
You should run the code below, but this would not set the accessflags, these should be set using the web.config of you’re ASP .NET 3.5 application:
<configuration> <system.webServer> <handlers accessPolicy="Read, Execute, Script">
If not set in the web.config the virtual directory will inherit the settings from the parent website
In this way you can create a virtual directory for a ASP .NET 3.5 application that works on IIS 6 and IIS 7
Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.DirectoryServices; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public string VirDirSchemaName = "IIsWebVirtualDir"; private string m_server; public string Server { get { return m_server; } set { m_server = value; } } private string m_virtualDirectoryName; public string VirtualDirectoryName { get { return m_virtualDirectoryName; } set { m_virtualDirectoryName = value; } } private string m_virtualDirectoryFolder; public string VirtualDirectoryFolder { get { return m_virtualDirectoryFolder; } set { m_virtualDirectoryFolder = value; } } private string m_websiteName; public string WebsiteName { get { return m_websiteName; } set { m_websiteName = value; } } private string m_appPoolId; public string AppPoolId { get { return m_appPoolId; } set { m_appPoolId = value; } } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.Server = "localhost"; this.VirtualDirectoryName = "TestVd11"; this.VirtualDirectoryFolder = @"C:\Temp"; this.WebsiteName = "Test"; this.AppPoolId = "TestAppPool"; this.CreateVirtualDirectory(); } /// <summary> /// Create an IIS virtualdirectory /// </summary> public void CreateVirtualDirectory() { int websiteId = this.GetWebSiteId(m_server, m_websiteName); try { using (DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc/{1}", m_server, websiteId))) { w3svc.RefreshCache(); foreach (DirectoryEntry child in w3svc.Children) { using (child) { child.RefreshCache(); if (child.Name.Equals("root", StringComparison.OrdinalIgnoreCase)) { // Remove virtual directory if it exists. DirectoryEntry vd = null; try { vd = child.Children.Find(m_virtualDirectoryName, VirDirSchemaName); if (vd != null) { vd.Invoke("AppDelete"); child.Children.Remove(vd); child.CommitChanges(); } } catch { } finally { if (vd != null) vd.Dispose(); } using (DirectoryEntry newVirDir = (DirectoryEntry) child.Invoke("Create", "IIsWebVirtualDir", m_virtualDirectoryName)) { newVirDir.Properties["AccessRead"][0] = true; newVirDir.Properties["AccessScript"][0] = true; newVirDir.Properties["AccessExecute"][0] = true; newVirDir.Properties["AppPoolId"][0] = this.AppPoolId; newVirDir.InvokeSet("Path", m_virtualDirectoryFolder.Trim('/')); newVirDir.Invoke("AppCreate", true); newVirDir.InvokeSet("AppFriendlyName", m_virtualDirectoryName); newVirDir.CommitChanges(); } } child.CommitChanges(); } } w3svc.CommitChanges(); } } catch (Exception ex) { Console.WriteLine(string.Format("Virtual Directory [{0}] already exists or error during creation. [{1}]", m_virtualDirectoryName, ex.ToString())); } } /// <summary> /// Get website id on websitename /// </summary> /// <param name="serverName">Name of the IIS server e.g. localhost, can be a netbios name, but can't be a host header</param> /// <param name="websiteName">Name of the website e.g. test</param> /// <returns> /// Less the 0, site does not exist /// Id of the existing site /// </returns> public int GetWebSiteId(string serverName, string websiteName) { if (string.IsNullOrEmpty(serverName)) { throw new Exception("Parameter [serverName] can't be null or empty"); } else { Console.WriteLine(string.Format("GetWebSiteId: [{0}]", serverName)); } if (string.IsNullOrEmpty(websiteName)) { throw new Exception("Parameter [websiteName] can't be null or empty"); } else { Console.WriteLine(string.Format("GetWebSiteId: [{0}]", websiteName)); } int result = -1; using (DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", serverName))) { w3svc.RefreshCache(); foreach (DirectoryEntry site in w3svc.Children) { using (site) { site.RefreshCache(); if (site.Properties["ServerComment"] != null) { if (site.Properties["ServerComment"].Value != null) { if (site.Properties["ServerComment"].Value.ToString().Equals(websiteName, StringComparison.OrdinalIgnoreCase)) { result = int.Parse(site.Name); } } } } } } Console.WriteLine(string.Format("GetWebSiteId: [{0}]", result)); return result; } } }