2016-11-23 60 views
2

我跟隨微軟的例子如何訪問使用文件夾選擇器選擇的文件夾。做完這些之後,我想要獲取所選頂層文件夾中所有子文件夾的路徑。但是我沒有得到任何結果。StorageFolder沒有得到任何數據與獲取文件夾async

代碼:

var folderPicker = new Windows.Storage.Pickers.FolderPicker(); 
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop; 
folderPicker.FileTypeFilter.Add("*"); 
folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List; 
Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); 

if (folder != null) 
{ 
    Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("GameFilesToken", folder); 
    System.Diagnostics.Debug.WriteLine(folder.Path); 

    await FileManagementHelper.getFolders(folder.Path); 
} 

public static async Task<Queue<string>> getFolders(String TopLevel) 
{ 
    var Folder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync("GameFilesToken"); 

    var queryResults = Folder.CreateFolderQuery(); 
    var folderList = await queryResults.GetFoldersAsync(); 

    return new Queue<string>(); 
} 

QueryResults讓我進入到一個文件夾,我原來使用相同的文件夾中。 FolderList沒有給我什麼。

回答

1

FolderList不給我任何東西。

你獲得folderList代碼片段可以很好地工作,folderList對象做,如果所選擇的文件夾中有子文件夾具有價值。請按以下步驟更新您的getfolders方法,通過代碼來檢查folderList

var Folder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync("GameFilesToken"); 
    var queryResults = Folder.CreateFolderQuery(); 
    var folderList = await queryResults.GetFoldersAsync(); 
    foreach (StorageFolder folder in folderList) 
    { 
     Debug.WriteLine(folder.Path); 
    } 

我想獲得選定的頂層文件夾中的子文件夾中的所有路徑。但是我沒有得到任何結果。

其實,你可以只使用StorageFolder.GetFoldersAsync()方法來獲取所有子文件夾選擇頂層文件夾的對象,也沒有必要使用CreateFolderQuery()方法,如果您想列出所有。代碼如下:

private async void btngetfolder_Click(object sender, RoutedEventArgs e) 
{ 
    var folderPicker = new Windows.Storage.Pickers.FolderPicker(); 
    folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop; 
    folderPicker.FileTypeFilter.Add("*"); 
    folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List; 
    Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); 

    if (folder != null) 
    { 
     IReadOnlyList<StorageFolder> folderList = await folder.GetFoldersAsync(); 
     foreach (StorageFolder subfolder in folderList) 
     { 
      Debug.WriteLine("subfolder path:" + subfolder.Path); 
     } 
    } 
} 

更多詳情請參考FolderEnumeration官方示例。

更新: 如果要獲取所有子文件夾包含子孫子文件夾,則需要將QueryOptions.FolderDepth設置爲較深。代碼如下:

var Folder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync("GameFilesToken"); 
QueryOptions options = new QueryOptions(); 
options.FolderDepth = FolderDepth.Deep; 
var queryResults = Folder.CreateFolderQueryWithOptions(options);  
var folderList = await queryResults.GetFoldersAsync(); 

foreach (StorageFolder folder in folderList) 
{ 
    Debug.WriteLine(folder.Path); 
} 
+0

我明白我的問題,唯一的是我需要得到頂級的所有目錄。因此,例如,TopLevel \,TopLevel \ Folder1,TopLevel \ Folder2,TopLevel \ Folder1 \ SubFolder1 –

+0

@BaileyMiller在這種情況下,您需要設置[FolderDepth](https://msdn.microsoft.com/en-us/庫/窗口/應用程序/ windows.storage.search.queryoptions.folderdepth.aspx)屬性爲深。詳情請看我最新的答案。 –

相關問題