获取Windows 8商店/ metro应用程序中某些文件扩展名的所有路径

时间:2014-11-25 06:50:07

标签: c# windows-8 windows-store-apps file-search

问题

我希望得到文件的所有路径" custom made"文件扩展名(.pssm和.pnsm)从一个文件夹及其每个子文件夹(深层搜索)在一个WINDOWS 8 STORE(TABLET)APP中。


THE STEPS

  1. 选择带有FolderPicker的父文件夹,然后保存文件夹路径字符串

    private async void BrowseButton_Tapped(object sender, TappedRoutedEventArgs e)
    {
        FolderPicker fPicker = new FolderPicker();
    
        fPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        fPicker.FileTypeFilter.Add(".pnsm");
        fPicker.FileTypeFilter.Add(".pssm");
    
        StorageFolder sFolder = await fPicker.PickSingleFolderAsync();
    
        if (sFolder != null)
        {
            StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", sFolder);
            (DataContext as MainPageVM).ParentFolder = sFolder.Path;
        }
    }
    
  2. 在以前保存的文件夹路径字符串及其子文件夹下搜索所有文件扩展名.pssm或.pnsm。


  3. 问题

    如何做第2步?


    其他详细信息

    这是我在我的桌面应用程序版本(桌面XAML应用程序,而不是Windows 8应用程序)中执行此操作的方法,也许它可以用作参考(我不知道如何使其适应以赢得8个应用程序)。

        private async void ButtonRefresh_Click(object sender, RoutedEventArgs e)
        {
            //http://www.codeproject.com/Messages/4762973/Nice-article-and-here-is-Csharp-version-of-code-fr.aspx
    
            counter = 0;
            string s = (DataContext as MainWindowVM).ParentFolder;
            List<string> exts = (DataContext as MainWindowVM).ExtensionToSearch;
    
            Action<int> progressTarget = new Action<int>(ReportProgress);
            searchProgress = new Progress<int>(progressTarget);
    
            List<string> queriedPaths =
                await Task.Run<List<string>>(() => GetAllAccessibleFiles(s, exts, searchProgress));
    
            (DataContext as MainWindowVM).RefreshList(queriedPaths);
            SaveSearch();
    
            progressText.Text += "(Search Completed)";
        }
    
        private List<string> GetAllAccessibleFiles(
                    string rootPath, List<string> exts, IProgress<int> searchProgress, List<string> alreadyFound = null)
        {
            if (alreadyFound == null)
            {
                alreadyFound = new List<string>();
            }
    
            if (searchProgress != null)
            {
                counter++;
                searchProgress.Report(counter);
            }
    
            DirectoryInfo dI = new DirectoryInfo(rootPath);
            var dirs = dI.EnumerateDirectories().ToList();
    
            foreach (DirectoryInfo dir in dirs)
            {
                if (!((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden))
                {
                    alreadyFound = GetAllAccessibleFiles(dir.FullName, exts, searchProgress, alreadyFound);
                }
            }
    
            var files = Directory.GetFiles(rootPath);
    
            foreach (string file in files)
            {
                if (exts.Any(x => file.EndsWith(x)))
                {
                    alreadyFound.Add(file);
                }
            }
    
            return alreadyFound;
        }
    

1 个答案:

答案 0 :(得分:0)

看来我只是有点接近答案。我不知道为什么我不介意。所以,这是我的RefreshButton_Tapped更新以获取所有路径。

    private async void RefreshButton_Tapped(object sender, TappedRoutedEventArgs e)
    {
        StorageFolder sFolder = 
            await StorageFolder.GetFolderFromPathAsync((DataContext as MainPageVM).ParentFolder);

        List<string> fileTypeFilter = new List<string>();
        fileTypeFilter.Add(".pnsm");
        fileTypeFilter.Add(".pssm");

        QueryOptions queryOptions =
            new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);

        StorageFileQueryResult results = sFolder.CreateFileQueryWithOptions(queryOptions);

        var files = await results.GetFilesAsync();

        List<string> paths = files.Select(x => x.Path.ToString()).ToList();

        SaveSearch();
    }

如果有人有更好的答案,请随意。