views:

199

answers:

2

I have a method that creates a virtual directory. How can I set the .Net framework to version 2 while I create the virtual dir?

My method looks like this so far:

private static void CreateVDir(string metabasePath, string vDirName, string physicalPath)
    {
        //  metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
        //    for example "IIS://localhost/W3SVC/1/Root" 
        //  vDirName is of the form "<name>", for example, "MyNewVDir"
        //  physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
        Console.WriteLine("\nCreating virtual directory {0}/{1}, mapping the Root application to {2}:",
                          metabasePath, vDirName, physicalPath);

        DirectoryEntry site = new DirectoryEntry(metabasePath);
        string className = site.SchemaClassName;
        if ((className.EndsWith("Server")) || (className.EndsWith("VirtualDir")))
        {
            DirectoryEntries vdirs = site.Children;
            DirectoryEntry newVDir = vdirs.Add(vDirName, (className.Replace("Service", "VirtualDir")));
            newVDir.Properties["Path"][0] = physicalPath;
            newVDir.Properties["AccessScript"][0] = true;
            // These properties are necessary for an application to be created.
            newVDir.Properties["AppFriendlyName"][0] = vDirName;
            newVDir.Properties["AppIsolated"][0] = "1";
            newVDir.Properties["AppRoot"][0] = "/LM" +
                                               metabasePath.Substring(metabasePath.IndexOf("/",
                                                                                           ("IIS://".Length)));

            newVDir.CommitChanges();

            Console.WriteLine(" Done.");
        }
        else
            Console.WriteLine(
                " Failed. A virtual directory can only be created in a site or virtual directory node.");
    }
+2  A: 

The "ScriptMaps" property is where the configuration mappings get stored. i.e. That's where you can map *.aspx files to get processed by ASP.Net. Here's an example.

David
I wanted to do in code the same thing that you can do in the IIS gui, when you open the virtual directory properties, do to ASP.NET page and select ASP.NET version from the dropdown. Does it do the same thing? Because potentially I will need to set it for different types of files, e.g. *.svc
Grzenio
Selecting the version from the ASP.Net dialog sets the script maps and does an IIS Reset. ALtering the ScriptMaps by code does the same thing.
Christopher_G_Lewis
A: 

After you have created the virtual directory, you can run aspnet_regiis -s from the .NET framework directory of your choice.

On my sytem, the command would look something like this:

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_regiis -s W3SVC/1/ROOT/SampleApp1

I have done this from a custom action in my installer successfully.

jlew