When I teach classes at Microsoft, I often precede a juicy tidbit of information or code sample with the statement “Here’s a good interview question for you.” Well, here’s a good interview question for you – especially if you want the interviewee out of your office as fast as humanly possible. How do you get a reference to a front-facing or back-facing camera on a Surface RT?

It’s easy to enumerate camera devices in WinRT, but distinguishing between front-facing and back-facing cameras requires a little more work. Here are three magic lines of code that will identify the front-facing camera (if it exists) and the back-facing camera (if it exists):

var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

var front = devices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);

var back = devices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);

This code works like a champ on a Surface RT, and will presumably work on other tablets as well.