[C#]Is it possible to copy a file in Assets to Local Folder in background thread? RRS feed

  • Question

  • I'm trying to do like this:

    private void Page_Loaded(object sender, EventArgs e)


    Task task = new Task(() =>


    StorageFile storageFile = await StorageFile.GetFileFromPathAsync("Assets/foo.dat");

    await storageFile.CopyAsync(ApplicationData.LocalFolder.Path, "foo.dat");

    // To do something It takes very long long time.

    // After initialized... jump to next page

    Windows.UI.CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(()=>{this.Frame.Navigate(typeof(NextPage); });




    I intend to copy a file from Assets to Local Folder and to do something about initialization.

    After that, jump to next page.

    But... There is a UnauthorizedException at storageFile.CopyAsync

    maybe the method works on UI Thread.

    How can I solve this problem?

    and I wonder this coding pattern is right. In Android, I used to code like this using runOnUiThread method.

    • Edited by ActiveX Destroyer Monday, February 13, 2017 8:38 AM supplement
    • Edited by David_FF Tuesday, February 14, 2017 8:41 AM title tag
    Monday, February 13, 2017 8:33 AM


All replies

  • Hi!

    In fact in UWP you have access on specifics paths because UWP application is a sandbox application.

    You should use ApplicationData.Current.LocalFolder; instead of ApplicationData.LocalFolder.Path.

    You'll find more informations in this topic : http://jamescroft.co.uk/blog/uwp/how-to-implement-local-storage-in-universal-windows-apps/

    Hope it wil be useful.

    If it's good for you, can you mark this answer as answer of your question please?

    Follow me on twitter : https://twitter.com/NordineMhoumadi

    My blog : https://nordinemhoumadi.wordpress.com/

    Monday, February 13, 2017 9:35 AM
  • That's my mistake.

    I already used ApplicationData.Current.LocalFolder.Path and It throws

    System.UnauthorizedAccessException: 'Access to the path 'C:\Users\***\AppData\Local\Packages\<AppID>\LocalState\foo.dat' is denied.'

    Thank you. I SOLVED.

    the file has read-only attribute and .NET Framework throws the exception when the file to delete is read-only.

    Monday, February 13, 2017 11:25 PM
  • Hi ActiveX,

    Thanks for sharing your solution with us. You could close the thread by marking yourself as answer if you have solved the issue. 

    Best Regards,

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 14, 2017 10:00 AM