Metro style app 文件查找(条件为用户输入的匹配条件)
private async void SearchButton_Click(object sender, RoutedEventArgs e) { StorageFolder musicFolder = KnownFolders.MusicLibrary; List<string> fileTypeFilter = new List<string>(); fileTypeFilter.Add("*"); QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderBySearchRank, fileTypeFilter); //use the user's input to make a query queryOptions.UserSearchFilter = InputTextBox.Text; StorageFileQueryResult queryResult = musicFolder.CreateFileQueryWithOptions(queryOptions); StringBuilder outputText = new StringBuilder(); //find all files that match the query IReadOnlyList<StorageFile> files = await queryResult.GetFilesAsync(); //output how many files that match the query were found if (files.Count == 0) { outputText.Append("No files found for '" + queryOptions.UserSearchFilter + "'"); } else if (files.Count == 1) { outputText.Append(files.Count + " file found:\n\n"); } else { outputText.Append(files.Count + " files found:\n\n"); } //output the name of each file that matches the query foreach (StorageFile file in files) { outputText.Append(file.Name + "\n"); } OutputTextBlock.Text = outputText.ToString(); }