UWP Copy Multiple Files from Assets to Specified Location

425 Views Asked by At

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?

2

There are 2 best solutions below

0
Mac On BEST ANSWER

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");
            }
        }
0
Devilboy Sixsixfive On

thank you very much! This really helped A LOT, but.

I have to answer my own question as I tweaked your tweak to get exactly where I was originally headed.

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\NewFolder1");

            foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
            {
                await assetFile.CopyAsync(projectFolder");
            }
        }

I actually needed a folder inside the "Assets" folder (@"Assets\NewFolder1");

And await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");

Actually saved everything in that folder as ".png" files with random names like "1234-456-789"

Therefore, was replaced with await assetFile.CopyAsync(projectFolder); To save everything with original extensions and names.

That's it!

Thanks again! x]