12,063,909 members (61,272 online)
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:
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.
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:
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.
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.
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.
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.
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.
I use this add-in all the time since writing it. Here's a few tips you may find useful: