On my computer I have a directory called Tutorial Sites and under that directory have a subfolder for various file system-based ASP.NET 2.0 websites I create to either have as a playground of sorts, to try out some new fangled feature of 2.0, or as a demo for class or article. What's kind of annoying is the process of creating and opening these sites. Yes, it's a lot quicker than creating a vdir in IIS and doesn't clutter the website metabase, but to create a file system-based website I need to launch VS 2005, click Open Website, then tinker down to the directory and choose a name. To open one, I have to do the same.
Most of the time I find myself using Windows Explorer to navigate around the file system. What I'd really like to be able to do is just right-click on a folder and say, “Open as Website” and, poof, Visual Studio would open and load that website. Clearly this makes opening an existing website as easy as pointing and clicking; to create a new website I'd just create a new folder through Windows Explorer, then open it in VS through the context menu.
Today I finally decided to stop wishing, invest some time, and actually get something that works. Here's what I tried:
- Visual Studio can be launched through the command-line by running devenv.exe. Therefore, my exploration started with examining devenv.exe's command-line switches. With an existing Solution or Project file, you can pass in the project or solution file and, voila, it will open. However, I didn't want a dependence on a project file - I just wanted to be able to right-click on a folder in Explorer and say, “Open as Website.“
- I did notice that VS can accept commands through the command-line switch. Commands can be activated through the Command Window in VS and allow for interacting with the GUI. For example, you can do File.OpenWebSite. However, this just pops up the Open Website dialog box - you still have to pick a file to open. And, sadly, you can't pass in an argument to this particular command. Shucks.
- I then though that maybe macros would solve my problem. I probably spent far too long poking through what little macro documentation I could find. Is there not a one-stop reference for the macro API?
- After banging my head on macros for a while, I decided to try something new, and ended up with a solution that works for me, but is a bit 'messy,' as you'll see in a moment.
Since Visual Studio will let you open a solution or project file passed through the command-line, I ended up writing a managed command-line program that could be passed the folder path of the website to open and a folder path to place a project file. This program would then create an actual project file and then launch devenv.exe, passing in this just-created project file.
What I don't like about my approach is that:
- It creates these 'garbage' project files that never get deleted unless you do so manually. Granted, these files are like 1-2KBs in size and can be tucked away in a directory out of sight, but it still feels dirty to do this.
- Second, I don't know how to get the path to devenv.exe, so the program has the path hard-coded (C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe). The program does accept an optional third input parameter, though, that can be used to specify the path to devenv.exe.
The complete code can be downloaded here. Once you have the code downloaded and compiled, to add the “Open as Website” option to the Windows Explorer context menu you'll need to run the .reg file included in the download. This adds a registry entry in HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\ that runs the program, passing in the path folder of that was just right-clicked. You will need to customize this .reg file before you run it, putting in the path to the program. You'll also need to specify the path to dump the project files. I chose the folder C:\Documents and Settings\Scott\My Documents\Visual Studio 2005\Projects\TempWebsiteProjects, but any path should suffice.
If you have any ideas or know-how on how to improve this, or run into any problems, please don't hesitate to post a comment to this blog entry. Special thanks to Chris Frazier and Robert McLaws, who both developed a “Start the ASP.NET 2.0 Development Web Server“ concept on their own. More info at Robert's “ASP.NET 2.0 Web Server Here“ Shell Extension.