System.UnauthorizedAccessException when creating file in UWP project

longevity uyl 285 Reputation points
2024-01-08T05:12:29.44+00:00

The following code can generate c:\temp\MyTest.bin successfully in Console project. But it failed to generate c:\temp\MyTest.bin in UWP project. Do you have any suggestion to fix this issue?

byte[] bytes = { 1, 2, 3 };
            string path = @"c:\temp\MyTest.bin";
            CFile.WriteBinaryFile(path, bytes);

        public static int WriteBinaryFile(string filePath, byte[] data)
Developer technologies | Universal Windows Platform (UWP)
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Junjie Zhu - MSFT 21,731 Reputation points
    2024-01-08T05:50:22.4233333+00:00

    Hi @longevity uyl ,

    Welcome to Microsoft Q&A!

    Different from the C# console app, UWP runs in the sandbox and cannot directly access external files. About how to create file in UWP, it is recommended that you refer to this document Create, write, and read a file.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

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