question

Kai-4867 avatar image
0 Votes"
Kai-4867 asked ·

[UWP][C++] winrt How to copy file from assets folder to local folder.

I want to copy a file from the Assets folder to the local folder. For this example lets use the LockScreenLogo.scale-200.png file as it commes with the default template. The docs give an example of file access here, https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions. If I modify that example to access LockScreenLogo and on MainPage.cpp it would look like

 Windows::Foundation::IAsyncAction MainPage::ExampleCoroutineAsync()
     {
         Windows::Storage::StorageFile file{
             co_await Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(Windows::Foundation::Uri{L"ms-appx:///Assets/LockScreenLogo.scale-200.png"})
         };
            
        
     }

This doesn't compile. And no examples are given to copy a file from Assets to the LocalFolder. Which I access else where with

auto location = winrt::Windows::Storage::ApplicationData::Current().LocalFolder();
winrt::Windows::Storage::StorageFolder Locationf{ location };

How can I copy a file from the assets folder to the LocalFolder?

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

When you get a file from the Assets folder, you can use CopyAsync() method to copy it to the local folder. For example:

 Windows::Foundation::IAsyncAction MainPage::ExampleCoroutineAsync( )
 {
         Windows::Storage::StorageFile file{
              co_await Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(Windows::Foundation::Uri{L"ms-appx:///Assets/LockScreenLogo.scale-200.png"})
          };
         co_await file.CopyAsync(ApplicationData::Current().LocalFolder());
    
 }


Thanks.



· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.