2014-11-25 40 views
0

的問題使用窗口的某些文件擴展名的所有路徑8店/ Metro應用

我想獲得的所有文件的路徑爲「定製」的文件擴展名(.pssm和.pnsm)從一個文件夾及其所有子文件夾(深度搜索)在WINDOWS 8 STORE(平板電腦)應用程序中。


步驟

  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。


問題

如何執行步驟#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; 
    } 

回答

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(); 
    } 

如果有人有更好的答案,隨時可以。

+0

使用查詢是執行此類搜索的最佳方式;比試圖自己遍歷文件夾結構要容易得多。 – 2014-11-25 15:01:47

+0

@ KraigBrockschmidt-MSFT有時默認迭代器會打「不安全」文件夾並返回錯誤。 – 2014-11-25 16:32:28

相關問題