Wednesday, February 29, 2012

Windows 8 and VS 2011 Beta

Microsoft did two exciting Beta releases today, Windows 8 (being called a “Consumer Preview” instead of  a Beta despite the fact that the default desktop background is a Betta fish), and Visual Studio 2011 Beta.

Windows 8 Consumer Preview

To download the new version you can get the web installer here or download the ISOs from here. I first tried running the web installer from an instance of the Windows 8 Developer Preview running in a VM but couldn’t do the install because I did not have enough space on the virtual C: drive. I decided to just create a new VM using VirtualBox 4.1.8, which now has a profile specifically for installing Windows 8, and install the Windows 8 CP from scratch. The install went very quickly and smoothly and installed without any problems on my first try.

My first impressions are that it seems faster then the previous version and the new tile based start menu works a little more smoothly. There is also a new task switcher that pops up along the left side of the screen when you press the Windows Key + Tab which is a nice feature. The classic desktop is still there, but the start menu button is now completely gone. You can still use the Windows key to switch back and forth between the desktop and the new start screen. I still don’t see how this new start screen is going to work out for desktop users, especially for power users.

Visual Studio 2011 Beta

You can download the Visual Studio 2011 Beta from here, and you can also get it from the MSDN Subscription downloads. I first tried installing the Ultimate version of the beta on my Windows Vista machine but it only supports Windows 7, 8 CP, Server 2008 R2 and Server 8 Beta. I am hoping this changes before the final release. I ended up installing the Beta on a Windows 7 virtual machine, and although the install took quite a while it went pretty smoothly. I also tried the install on a Windows 8 CP VM which, despite some information I saw to the contrary, is support.

I haven’t had to much time to play with the VS 11 Beta yet, but the one thing that is very noticeable when you start it up is the new, very Metro-like  interface. It seems like a cleaner interface then the previous version, but I will need to work with it for a while to see if it’s really any better.

Thursday, November 24, 2011

WinRT File Access

In my last post I showed how to use an OpenFilePicker in a WinRT application to get access to a file selected by the user. Now let’s take a look at how you access the file system directly without prompting the user. We will start with a simple application that lists the names of the files in the user’s Picture Library.
Start by creating a C# Windows Metro Style Application. Within the Grid element add the following XAML to create textblock we can use to display the name.

<TextBlock x:Name="TextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top"/>

In the code behind add the following code to handle the UserControl_Loaded event:

private async void UserControl_Loaded(object sender, RoutedEventArgs e)
{

var folder = KnownFolders.PicturesLibrary;
var files = await folder.GetFilesAsync();

foreach (StorageFile file in files)
{ 
    TextBlock.Text += file.FileName + ":" + file.ContentType + "\n"; 
}

}

Before running the code, select Exceptions from the Debug menu and check Common Language Runtime Exceptions Thrown. Now run the application and you will notice that it throws a System.UnauthorizedAccessException. Why does this happen?

As I mentioned in my last post, WinRT apps run in a pretty strict sandbox and by default have very limited access to the file system. We got around this last time because we let the user pick a file, so they would have full knowledge of what they are allowing the application to access. But in this case, since we are directly accessing the file system, we get an exception. To solve this problem, double click Package.appxmamnifest in the solution explorer the click on the Capabilities tab.

clip_image002

On this screen we can specify what we want application to be allowed to do. To give the application access to the Picture Library, put a check next to Picture Library Access. If you then re-run the application you will not get an exception and a list of the pictures will be displayed. At first this might seem a little pointless. If the programmer can just click a check box to get access it’s not particularly good security. The value is that it will make it much easier for the prospective user of the app to know what the app is allowed to do. Although we haven’t seem Microsoft’s Windows 8 app store yet, it’s entirely possible that when we look at the information about an app before we purchase it, we will able to see a list of these Capabilities so we know exactly what the app has access to.

Now that we understand about Capabilities, lets look at how the code actually works. In the first line we create a new folder object from one of the KnownFolders. In this case the code selects the PictureLibrary, but we could also select folders like the MusicLibrary, VideosLibrary, or DocumentLibrary.

Once we have the folder object the second line calls it’s GetFilesAsync method to return a list of StorageFile objects for the files in the folder. As always with WinRT, if a method could potentially take more the 50ms to run, it’s executed asynchronously. The next three lines iterate through the list of StorageFiles and display their names.