virtual directory properties in c#
<p>I did a quick search and got this link
<a href="http://stackoverflow.com/questions/1243600/creating-virtual-directory-in-iis-with-c-sharp">From StackOverflow</a></p>

<p>I get this error message
"Unknown error (0x80005000)"
when it hits </p>

<pre><code>bool canCreate = !(schema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN");
</code></pre>

<p>My requirement is to create a virtual directory and set AccessRead, IsolationMode, Scripts and Executables, ApplicationProtection to medium.</p>

<hr>

<p>Got it to work, here is the code:</p>

<p>Note: I still couldn't get it to work on windows 7, it throws the same error and the message includes reference to System.InterOp... At the moment I don't care about getting it to work on windows 7, I deployed it on windows 2003, it works.</p>

<pre><code>public void CreateVirtualDirectory(string virtualdirectory, string physicalpath)
{
    try
    {
        ///check if path exists
        if (!Directory.Exists(physicalpath))
        {
            Log(string.Format(@"CreateVirtualDirectory; Path not found - {0}", physicalpath));
            return;
        }

        DirectoryEntry parent = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/1/Root", Environment.MachineName));
        foreach (System.DirectoryServices.DirectoryEntry v in parent.Children)
        {
            if (v.Name == virtualdirectory)
            {
                try
                {
                    parent.Invoke("Delete", new string[] { v.SchemaClassName, virtualdirectory });
                }
                catch
                {
                }
            }
        }
        DirectoryEntry newFolder = (DirectoryEntry)parent.Invoke("Create", "IIsWebVirtualDir", virtualdirectory);
        newFolder.InvokeSet("Path", physicalpath);
        newFolder.Invoke("AppCreate", false);
        newFolder.InvokeSet("AppFriendlyName", virtualdirectory);

        const int MEDIUM_POOL = 2;

        newFolder.Properties["AccessRead"][0] = true;
        newFolder.Properties["AccessExecute"][0] = true;
        newFolder.Properties["AccessWrite"][0] = false;
        newFolder.Properties["AccessScript"][0] = true;
        newFolder.Properties["AuthNTLM"][0] = true;
        newFolder.Properties["AppIsolated"].Clear();
        newFolder.Properties["AppIsolated"].Add(MEDIUM_POOL);

        newFolder.CommitChanges();
    }
    catch (Exception ex)
    {
        Log(string.Format(@"CreateVirtualDirectory '{0}' failed; {1}", virtualdirectory, ex.Message));
    }
}
</code></pre>
