Sunday, August 25, 2013

I was looking sample source code on windows 8 and I found this collection of 159 windows 8 app samples in C#. You can also find windows 8 and 8.1 app samples in c#, vb.net, C++ and Javascript. Many windows 8 developers might be aware of it already but it becomes painful after some time to go and open each solution. However, I think most people only open a particular solution when the need arises. However, I wanted to explore all of them (159 of them), run, and see what all the different features Windows 8.1 and 8 SDK has. That way I am at least aware of the features and use them in my application whenever needed. The only problem is opening each of them through the explorer window and locate .sln file three layers deep. One more thing I am doing this all on a surface pro sitting while on a couch.

Part of my frustration with these samples was, first you have to download them samples (that’s ok), unblock them since they are downloaded from internet (fine by me), then navigate to the solution folder and open the .sln file (not good for 159 of them). There is a lot of context switching when I launch the application from Visual Studio and then the modern interface opens up another context switch. Now try to do this on a Windows 8 pro tables like the Surface Pro while sitting on your couch or lying in the bed. Too much hassle right, but I have figured out an easy way to overcome this problem. My Answer to the problem is Powershell. I have attempted to solve this problem before(link) but it was not as elegant as this one is.
Check this powershell one-liner

I extracted windows8 samples to the folder C:\windows8samples. This one-liner first finds all the .sln files in the samples folders, then selects just the name and fullname property of the the .sln file, and then sorts them by name, and then shows you list of .sln files in a gridview window. If you are running windows 8 for viewing all the windows 8 samples, you have powershell v3. In PS v3, you can pass the object(s) you select from the gridview down to the on going pipeline. You can select a single object which what I have done here because I do not want to open multiple solutions. This is done by the –Outputmode single property. After selecting the object in the gridview interface you click ok and then I use invoke item command to open the file in the default program configured for the file.

Ok so what's simple in this as you might ask right. Copy this one-liner, create a powershell script named OpenVSSolutions.ps1, and create a shortcut on the desktop. Open the properties of the shortcut file and change the target field in the property window to this

This will hide powershell window and present you gridview with all solutions and then you can select one solution and hit ok. It will open visual studio. Done. Then just run the application and explore. To ease surfing source code inside visual studio 2012 or 2013 is switch scroll to use map mode scrolling (now supported in VS2013 and productivity power tool for VS2012).

You can change icon of your script too to reflect something else than powershell icon. Mine looks like the following.

You can place the shortcut on the toolbar or pin it to start page.

Now that is simple and easy solution to browse all samples quickly using touch on a Surface Pro lying in the bed when you don’t want to sit at your desk.

This solution works for other purposes too. I have folder named C:\BadSourceCodes\ which contains ad-hoc solutions I create to quickly test a concept. Over a time the folder contains lots of solutions and finding them is also pain. Instead I use above technique to open solutions. I have couple of such folders with lots of Visual Studio solutions some contains samples from various books I have purchased over the years, my production source code library, badsourcecode library and others. To make our experiment more generic lets change our one line of powershell to replace the hardcoded path with a $path variable. Save the script file.

Now you can create multiple shortcuts for your folders and edit target property of each shortcut.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -NonInteractive C:\Scripts\OpenVSSolutions.ps1 “C:\BadSourceCode"