/// <summary> /// This is the click handler for the Search button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async private void Search_Click(object sender, RoutedEventArgs e) { OutputProgressRing.IsActive = true; Windows.Storage.Search.QueryOptions queryOptions = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.OrderBySearchRank, null); queryOptions.UserSearchFilter = QueryInputBox.Text; // This try/catch block is for scenarios where the Homegroup Known Folder is not available. try { Windows.Storage.Search.StorageFileQueryResult queryResults = Windows.Storage.KnownFolders.HomeGroup.CreateFileQueryWithOptions(queryOptions); System.Collections.Generic.IReadOnlyList <Windows.Storage.StorageFile> files = await queryResults.GetFilesAsync(); if (files.Count > 0) { string outputString = (files.Count == 1) ? "One file found\n\n" : files.Count.ToString() + " files found\n\n"; foreach (Windows.Storage.StorageFile file in files) { outputString += file.Name + "\n"; } rootPage.NotifyUser(outputString, NotifyType.StatusMessage); } else { rootPage.NotifyUser("No files found.", NotifyType.StatusMessage); } } catch (Exception ex) { rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); } OutputProgressRing.IsActive = false; }
/// <summary> /// This is the click handler for all four buttons in this example. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async private void Default_Click(object sender, RoutedEventArgs e) { Button b = sender as Button; if (b != null) { OutputProgressRing.IsActive = true; // Each visible button was previously set to display a homegroup user's name string userName = b.Content.ToString(); // This try/catch block is for scenarios where the Homegroup Known Folder is not available. try { System.Collections.Generic.IReadOnlyList <Windows.Storage.StorageFolder> hgFolders = await Windows.Storage.KnownFolders.HomeGroup.GetFoldersAsync(); bool userFound = false; foreach (Windows.Storage.StorageFolder folder in hgFolders) { if (folder.DisplayName == userName) { // We've found the folder belonging to the target user; search for all files under it userFound = true; Windows.Storage.Search.QueryOptions queryOptions = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.OrderBySearchRank, null); queryOptions.UserSearchFilter = "*"; Windows.Storage.Search.StorageFileQueryResult queryResults = folder.CreateFileQueryWithOptions(queryOptions); System.Collections.Generic.IReadOnlyList <Windows.Storage.StorageFile> files = await queryResults.GetFilesAsync(); if (files.Count > 0) { string outputString = (files.Count == 1) ? "One file found\n\n" : files.Count.ToString() + " files found\n\n"; foreach (Windows.Storage.StorageFile file in files) { outputString += file.Name + "\n"; } rootPage.NotifyUser(outputString, NotifyType.StatusMessage); } else { rootPage.NotifyUser("No files found.", NotifyType.StatusMessage); } } } if (!userFound) { rootPage.NotifyUser("The user " + userName + " was not found on the HomeGroup.", NotifyType.ErrorMessage); } } catch (Exception ex) { rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); } OutputProgressRing.IsActive = false; } }
/// <summary> /// Returns an instance of the StorageFile for the specified filename in the specified storage folder (overloaded). /// </summary> /// <param name="filename">The instance of the StorageFolder.</param> /// <param name="Folder">The name of the file.</param> /// <returns>Instance of StorageFile</returns> public static async Task <StorageFile> GetFile(string filename, StorageFolder Folder) { StorageFile file = null; // Set the default parentFolder to the LocalFolder, if not defilned. var dataFolder = Folder ?? Windows.Storage.ApplicationData.Current.LocalFolder; // Get the files available from the localFolder Windows.Storage.Search.StorageFileQueryResult fileResults = dataFolder.CreateFileQuery(); // Get the list of files from the query against the localFolder IReadOnlyList <StorageFile> fileList = await fileResults.GetFilesAsync(); // Look for our file in the results. file = fileList.SingleOrDefault(f => f.Name == filename); return(file); }
public async Task <StorageFile> GetFile(string filename) { StorageFile file = null; // Access variable for the local storage system, where we're going to store the Student records. Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; // Get the files available from the localFolder Windows.Storage.Search.StorageFileQueryResult fileResults = localFolder.CreateFileQuery(); // Get the list of files from the query against the localFolder var fileList = await fileResults.GetFilesAsync(); // Look for our file in the results. file = fileList.SingleOrDefault(f => f.Name == filename); return(file); }