HomeGroup
Microsoft provides a special service named HomeGroup that is designed to make it easier to share folders, files, and devices on home networks. If you are not familiar with HomeGroup, Microsoft provides an online tutorial to help you set one up “from start to finish.”1 The Windows shell handles the special network behind the scenes and exposes it as a file system in Explorer.
Figure 10.1 shows an example folder in the HomeGroup. Notice that the initial set of “folders” corresponds to users on the network, followed by the machines they are logged into. These, in turn, expose libraries based on the user’s preferences for sharing pictures, documents, music, or other items. You can browse to the folders you have permissions for and access the items as you normally would.
FIGURE 10.1 The HomeGroup network
The HomeGroupExample project for Chapter 10 demonstrates access to the HomeGroup. The first step is to declare your capabilities in the package manifest. You must have at least one of the available library capabilities (music, pictures, or videos) checked, or you will receive an access denied exception when you attempt to access the HomeGroup. Otherwise, you will have access only to the folder types that you specified capabilities for.
Use the KnownFolders.HomeGroup enumeration to access the HomeGroup network. The first set of folders you receive is mapped to the usernames of users currently participating in the HomeGroup. The following code in the Initialize method of the ViewModel class fetches the user-level folders:
var folders = await Windows.Storage.KnownFolders .HomeGroup.GetFoldersAsync();
The example project defines the HomeGroupUser class for user information and maps the DisplayName attribute of the folder to the username displayed.
foreach (var user in folders.Select( folder => new HomeGroupUser { UserName = folder.DisplayName, IsHomeGroupUser = true })) { this.Users.Add(user); }
When you have a StorageFolder instance for the user, you can use queries to iterate items within the folder. This query sets up a search for pictures with a known set of filename extensions and ultimately retrieves any shared photos that user is sharing across all devices on the HomeGroup.
var query = new QueryOptions(CommonFileQuery.OrderBySearchRank, new[] { ".jpg", ".png", ".bmp", ".gif" }) { UserSearchFilter = "kind:picture" }; var files = await targetFolder .CreateFileQueryWithOptions(query).GetFilesAsync();
The app is designer-friendly and shows a sample image and title in the designer. When you run the app, you see either an error message displayed on a disabled button if the app cannot access a valid HomeGroup, or a list of buttons for each user on the HomeGroup. Tap the button to see the images that user is sharing. You can use similar functionality as covered in Chapter 4, “Data and Content,” to access other folders and content types.