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

Kai 81 Reputation points
2019-12-10T19:23:54.137+00:00

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://learn.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?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Fay Wang - MSFT 5,221 Reputation points
    2019-12-11T03:04:40.793+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.