White55
White55

Reputation: 97

Save the folder path for loading UWP

Save the folder path for loading

I uploaded images from a folder selected by a FilePicker but, I would like (after the first time I chose the folder) that when I start the app, automatically loads the selected folder, without having to retrieve it from the picker file every time.

MainPage.xaml:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button x:Name="btnPickFolder" 
            Content="Pick Folder" 
            Click="btnPickFolder_Click" 
            HorizontalAlignment="Left" 
            Margin="10,10,0,0" 
            VerticalAlignment="Top"/>
    <Grid x:Name="GridShowImages" HorizontalAlignment="Stretch" Margin="20,52,20,20">
        <GridView x:Name="ListViewImage" ItemsSource="{x:Bind listImage}">
            <GridView.ItemTemplate>
                <DataTemplate x:DataType="local:SingleImage">
                    <Image Source="{x:Bind ImageToLoad}" 
                           Margin="5" 
                           Width="300" 
                           Height="168.75"/>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>
    </Grid>
</Grid>

MainPage.xaml.cs:

public sealed partial class MainPage : Page
{
    ObservableCollection<SingleImage> listImage = new ObservableCollection<SingleImage>();

    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void btnPickFolder_Click(object sender, RoutedEventArgs e)
    {
        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        folderPicker.FileTypeFilter.Add("*");
        StorageFolder SelectFolderToLoad = await folderPicker.PickSingleFolderAsync();
        StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", SelectFolderToLoad);

        foreach (var file in await SelectFolderToLoad.GetFilesAsync())
        {
            BitmapImage bmp = new BitmapImage();
            IRandomAccessStream stream = await file.OpenReadAsync();
            bmp.SetSource(stream);
            listImage.Add(new SingleImage() { ImageToLoad = bmp });
            StorageFolder StorageParent = await file.GetParentAsync();
        }
    }
}

SingleImage class:

public class SingleImage
{
    public BitmapImage ImageToLoad { get; set; }
}

Upvotes: 2

Views: 855

Answers (1)

Dave Smits
Dave Smits

Reputation: 1879

you can use the FutureAccesList (https://learn.microsoft.com/en-us/uwp/api/Windows.Storage.AccessCache.StorageApplicationPermissions#Windows_Storage_AccessCache_StorageApplicationPermissions_FutureAccessList) to keep access to the selected folder / file after your app restarts

Upvotes: 2

Related Questions