At SlickEdit, we've put several "advertorials" out now on The Code Project about our Tools for Visual Studio product. I'm one of the developers on the Tools team [profile], and I love The Code Project for learning how to do new things, especially because I learn best by seeing examples of how something's done. However, in order to see the sample code for an article, you have to:
- Download the sample ZIP file
- Browse to it and unzip the contents somewhere
- Go to Visual Studio and Open the project
I also love convenience, and that's why I made this add-in. It lets you browse The Code Project directly in Visual Studio 2005 or 2008. When you click on a link that would normally download a zip file, it asks you instead if you'd like to open the sample up using The Code Project Browser. If you say yes, the add-in will download the file, unzip it to a base My Documents\My Code Project Samples directory, then load it directly. It also provides a sidebar where you can view, reload and delete all of the projects you've downloaded.
How It's Used
Once installed, you will have a Tools > Code Project Browser menu item. Clicking that will bring up the add-in, which runs by default as a tabbed document (you can change that by right clicking the tab). NOTE: You may need to use the add-in manager if it doesn't initially show up.
You can browse The Code Project normally using the add-in. Once you click on a sample zip file link, the following will happen:
- You will be prompted to open the project using the Code Project Browser. If you say no here, then the browser will handle your request and can save the zip file like you normally do. NOTE: You must be logged into the Website to download files from The Code Project.
- If you say yes, then the add-in will download the project and unzip it to My Documents\My Code Project Samples\[name].
[name] is derived from the unique directory that the article and sample reside in on The Code Project.
- The add-in will inspect the new directory for any solutions, and if one is found, it will load it directly. If no solution is found, it will look for any project files, and if one or more are found, it will load the first one directly.
- An entry is created in the "Downloaded Projects" view for the new directory. All solutions, projects and sample executables will be included under the new project tree node.
This area allows you to view, reload and delete any of the projects you've downloaded from The Code Project. To go to the "Downloaded Projects" view, click the "Downloaded Projects View" toolbar button. It's a sidebar tree view that shows all of the directories in the My Documents\My Code Project Samples directory and shows all of the solutions and projects they contain.
You can reload any project or solution from this view by double clicking on the item you want to load. There is a context menu available for all items that lets you browse to the project's article or its containing directory. You can also delete projects by selecting the directory node and pressing the delete key, or by right clicking and selecting Delete.
- Downloaded Projects View: Shows the sidebar window with downloaded projects
- Back: Navigates backwards in the history list
- Forward: Navigates forwards in the history list
- Home: Navigates to the main CodeProject Web page
- Print Current Page: Prints the current Web page
- URL bar: Shows the current article URL. Also lets you type in a page to navigate to manually
- Navigate: Navigates to the location in the URL bar
Using the Code
All of the code for this add-in is included in the sample project at the top of this article. Because there are several articles already about writing add-ins, I'll go over some of the things that I found particularly interesting while working on this project.
This code uses the
SharpZip library by ic#code to unzip the contents of downloaded ZIP files. It's always been frustrating that ZIP capability was not added to the .NET libraries, but these guys have done a great job with this library. It's available at the SharpZip Web page.
The Tool Window "Shim"
I hadn't been working on this for 15 minutes before I hit a major roadblock. I had no idea how to do something so simple as create a tool window as part of my add-in. This was unbelievably frustrating; how could this be so hard? Thirty minutes of Help browsing came up with nothing and it wasn't until I went on the Web that I found Carlos Quintero's article about how to do this. In a nutshell, creating a tool window involves using a homegrown "shim" or pseudo-host for the tool window contents, written by Craig Skibo from the Visual Studio team. I'd like to thank both of them for their published work.
I later found this article, which describes the new way to do this in Visual Studio 2005 without using the shim. Apparently, the shim was required for Visual Studio 2003, but not Visual Studio 2005. All of this goes to show one thing that I've learned from doing add-in and VSIP work... the solutions to most problems are rarely obvious or intuitive and the importance of community contributors is huge.
Downloading and Being Logged In
Again, I thought that something that would be very straightforward turned out to be quite a challenge. I intercept the Web browser's "Navigating" event and if the URL has a *.zip extension, then I assume that it is sample code and prompt the user whether or not they want to open it directly in Visual Studio. If they say yes, then I use the
WebResponse mechanism to download the file. Sounds easy, right?
Unfortunately, The Code Project requires that you are logged in to download samples. If you log in through the Web browser, then the Web browser has established a session with The Code Project and identifies its session with a cookie, obtained at the time of the log in. Without that cookie, The Code Project thinks that the request is coming from an unknown source, and redirects to the login page. Because of this, trying to download files this way resulted in lots of downloaded login pages.
It wasn't until I found this post by Durgaprasad Gorti that I learned how this could be done using the
Those who used the The Code Project Browser before remember that there used to be a special separate login to use the add-in. I recently found a way to parse the needed cookies directly from the Web page owned by the Web Browser control. That means that the separate login is no longer required and you log in through the Web site, which is how I wanted it originally.
Reporting Download Progress
Both logging in and downloading files are done asynchronously using the
BackgroundWorker class. One thing that I really wanted to include was progress reporting while downloading larger ZIP files. However, when requesting a ZIP file for download from The Code Project, the size of the download is not sent back in the header of the response. Consequently, without that information there's no good way to report progress. My solution was to estimate a download size of 1 meg, which is larger than most CodeProject downloads. If the size is greater than 1 meg, then the progress bar loops. It's not perfect, but at least it's a good way to at least show that progress is being made.
Finally, a word about the DTE. I needed to use this object to have Visual Studio load any downloaded projects or solutions. The
DTE.Solutions collection has the
AddFromFile() functions, which looked perfect for doing this. Unfortunately, these functions failed horribly whenever a Visual Studio 2002/2003 project or solution version was passed as an argument.
I tried invoking the
File.OpenProject command, passing the name of the project or solution. This seems to go through the proper channels and correctly launches the Conversion Wizard. It's good to get to know the commands available in Visual Studio, from both an add-in development point of view and also for being able to speed up your own productivity by binding keys to those operations. SlickEdit offers the
CommandSpy as part of our free Gadgets download, which I use extensively and highly recommend.
Points of Interest
I use this add-in all the time since writing it. Here's a few tips you may find useful:
- By default, the tool Window exists as a tabbed document so that it gets lots of real estate. I have a multi-monitor setup, and I like to float it by right clicking on the tab and selecting "Floating". I then put the tool window on the second monitor while I work in the first. That way, I get even more real estate and I can look at the article while I look at the code.
- If you say "yes" to the add-in's prompt to open a ZIP file in Visual Studio, it may not have any projects, solutions or executables inside. This is the case with ZIP files that just contain a few code files. You can still save the ZIP like you normally would by clicking "no" when prompted by the add-in. This will forward the call to the Web browser and you'll be able to save the file normally.
- This add-in works with Visual Studio 2005 and 2008, but will not work with Visual Studio 2003.
- 10 Aug 2007 - Original posting
- 16 Aug 2007 - People with non-traditional C:\Program Files\ directories were not able to use the add-in after installation, because the *.addin file pointed to an incorrect directory. I used the article by Ting Huang to update the installer to handle this case.
- 24 Mar 2008 - Updated the installer to work with Visual Studio 2008. You can also install the add-in anywhere you like. No separate login is needed anymore, you just need to be logged into the site. This eliminates the need for favorites. An address bar was added, as well as a context menu to the project tree so that you can easily refind the article associated with a project download.