At work I have a need to switch where a virtual directory on my local system is pointed; it relates to the fact that we have to manage several branches of the same web project and I need to be able to change the physical path of a virtual directory on the fly in order to view the status of changes / merges. At first I used the symbolic links in Windows to achieve this, but this technique has a big drawback: Visual Studio’s integrated source control freaks out when you do this. Well it works, but the auto check out /check in from within the IDE does not.
My new approach is going to be dynamically changing where the virtual directory in IIS is pointed. I hope that this will fix all the issues in dealing with the different branches of code and the issues with visual studio.
So I wrote this little C# application that installs a system tray icon—this changes the virtual directory from a right click menu. It reads a configuration file that has all the mappings that I will need to map all the different branches in IIS. I’m not sure if anyone else could benefit from this, but here is the source code. Please forgive the condition it’s in, I wrote it in just a few minutes.
*note: This is a visual studio 2008 solution.
**note: I have found that I need to have the ability to have these grouped by the project.
More Details:
If you don't wish to download the source code and but you would still like to see how this is done, here are a couple of snippets. This first function is to create a virtual directory, this was taking right out of the source code and not altered, to view this in context then grab the source and have a look.
public static void CreateVirtualDirectory(VirtualDirectory virtualDirectory)
{
//Only allow one of these to be active at a time.
ClearAllVirtualsFromConfig();
DirectoryEntry websiteRoot;
DirectoryEntries websiteRootDirectoryCollection;
DirectoryEntry newVirtualDirectory;
try
{
// this is the root website... if you want to create a dir in a different site... then this must change.
websiteRoot = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
websiteRootDirectoryCollection = websiteRoot.Children;
newVirtualDirectory = websiteRootDirectoryCollection.Add(virtualDirectory.VirtualDirectoryName, websiteRoot.SchemaClassName.ToString());
newVirtualDirectory.CommitChanges();
//Create physical path if it does not exists
if (!Directory.Exists(virtualDirectory.PhysicalPath))
{
Directory.CreateDirectory(virtualDirectory.PhysicalPath);
}
// Set virtual directory properties
newVirtualDirectory.Properties["Path"].Value = virtualDirectory.PhysicalPath;
newVirtualDirectory.Properties["AccessRead"][0] = true;
newVirtualDirectory.Invoke("AppCreate", true);
newVirtualDirectory.Properties["AppFriendlyName"][0] = virtualDirectory.VirtualDirectoryName;
//Other Options
//newVirtualDirectory.Properties["AccessExecute"][0] = true;
//newVirtualDirectory.Properties["AccessWrite"][0] = true;
//newVirtualDirectory.Properties["AccessScript"][0] = true;
//newVirtualDirectory.Properties["AspEnableParentPaths"][0] = true;
//newVirtualDirectory.Properties["AuthNTLM"][0] = true;
//newVirtualDirectory.Properties["DefaultDoc"][0] = "default.aspx";
//newVirtualDirectory.Properties["EnableDefaultDoc"][0] = true;
//newVirtualDirectory.Properties["EnableDirBrowsing"][0] = true;
newVirtualDirectory.CommitChanges();
}
catch (Exception ex)
{
throw ex;
}
}
This second block of code is how to delete virtual directories in IIS.
public static void DeleteVirtualDirectory(VirtualDirectory virtualDirectory)
{
System.DirectoryServices.DirectoryEntry websiteRoot;
System.DirectoryServices.DirectoryEntries websiteRootDirectoryCollection;
try
{
websiteRoot = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
websiteRootDirectoryCollection = websiteRoot.Children;
websiteRootDirectoryCollection.Remove(websiteRootDirectoryCollection.Find(virtualDirectory.VirtualDirectoryName, websiteRoot.SchemaClassName.ToString()));
websiteRoot.CommitChanges();
}
catch (Exception ex)
{
throw ex;
}
}
*note that some of this code was derived from this article on the code project. Also noted in the source code.