【问题标题】:Get all path of certain file extensions in windows 8 store / metro application获取 Windows 8 商店/metro 应用程序中某些文件扩展名的所有路径
【发布时间】:2015-01-23 02:00:33
【问题描述】:

问题

我想在 WINDOWS 8 STORE (TABLET) 应用程序中从文件夹中获取具有“自定义”文件扩展名(.pssm 和 .pnsm)的文件的所有路径,以及它的每个子文件夹(深度搜索) .


步骤

  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 的文件。


问题

第二步怎么做?


其他详情

这就是我在我的桌面应用程序版本(桌面XAML应用程序,不是windows 8 one)中的做法,也许它可以作为参考(我不知道如何适应它以适应win 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;
    }

【问题讨论】:

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


    【解决方案1】:

    看来我离答案有点近了。我不知道为什么我没有想到。所以,这是我的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();
        }
    

    如果有人有更好的答案,请随时提出。

    【讨论】:

    • 使用查询是进行此类搜索的最佳方式;比尝试自己遍历文件夹结构要容易得多。
    • @KraigBrockschmidt-MSFT 有时默认迭代器会命中“不安全”文件夹并返回错误。
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多