Home > Net >  UWP - How do I get StorageFolder from FolderInformation in C#?
UWP - How do I get StorageFolder from FolderInformation in C#?

Time:10-07

I am using FileInformationFactory.GetVirtualizedFoldersVector method to populate a ListView control. GetVirtualizedFoldersVector returns a vector of IStorageItemInformation objects which in the folder's case happens to be Windows.Storage.BulkAccess.FolderInformation.

In the clicked item event handler I want to get the corresponding StorageFolder

In C /WinRT I can use

void MainPage::FolderListView_ItemClick(Windows::Foundation::IInspectable const&, Windows::UI::Xaml::Controls::ItemClickEventArgs const& args)
{
    auto clickedItem = args.ClickedItem();
    if (clickedItem)
    {
        auto folder = clickedItem.try_as<StorageFolder>();
        if (folder)
        {
        // We have a StorageFolder
        }
    }
}

This works perfectly but the same thing in C# does not work !

private void FolderListView_ItemClick(object sender, ItemClickEventArgs args)
{
    var clickedItem = args.ClickedItem;
    if (clickedItem != null)
    {
        StorageFolder folder = clickedItem as StorageFolder;
        if (folder != null)
        {
           // folder is always null
        }
    }
}

I don't want to use StorageFolder.GetFolderFromPathAsync() because the path is empty ("") for virtualized locations like KnownFolders.MediaServerDevices

CodePudding user response:

Try casting it to a specific interface such as IStorageFolder rather than the class.

  • Related