uwpcopystoragefilestoragefolder

UWP Copy Multiple Files from Assets to Specified Location


So I've made it this far:

private async void DownloadButton_Click(object sender, RoutedEventArgs e)
   {
    // Pick a location to create new folder in.

    FolderPicker picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.Downloads };
    picker.FileTypeFilter.Add("*");
    StorageFolder folder = await picker.PickSingleFolderAsync();

    // Create new folder with "custom name" + replaces existing.

    var projectFolderName = "New Folder 2";
    StorageFolder projectFolder = await folder.CreateFolderAsync(projectFolderName, CreationCollisionOption.ReplaceExisting);

    //Pick a file to be copied

    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/NewFolder1/File1.png"));
    StorageFile file2 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/File2.png"));

    //Paste copied file in custom folder.

    await file.CopyAsync(projectFolder, "File1.png");
    await file2.CopyAsync(projectFolder, "File2.png");
    }
  }
}

What I can't figure out if how to get all the files at once and copy them all together.

I can write new line of copy/paste for each file there is, but there has to be easier way to put this all together.

Thanks?


Solution

  • What I can't figure out if how to get all the files at once and copy them all together.

    Here's one way to achieve that using your code:

    First, you need to get the Assets (Source) folder.

    You'll be able to do that using this line:

    StorageFolder assetsFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");
    

    Then enumerate the files from the Source and copy to the Specified Folder (Destination)

     foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
     {
         await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");
     }
    

    Here's the tweaked code:

    private async void DownloadButton_Click(object sender, RoutedEventArgs e)
            {
                // Pick a location to create new folder in.
                FolderPicker picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.Downloads };
                picker.FileTypeFilter.Add("*");
                StorageFolder folder = await picker.PickSingleFolderAsync();
    
                // Create new folder with "custom name" + replaces existing.
    
                var projectFolderName = "New Folder 2";
                StorageFolder projectFolder = await folder.CreateFolderAsync(projectFolderName, CreationCollisionOption.ReplaceExisting);
    
                // Copy all files from assets folder and paste to destination.
                StorageFolder assetsFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");
    
                foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
                {
                    await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");
                }
            }