1

I'm writing a text editor in MAUI and encountered a problem that when executing this code (on Windows 10), the error "Exception raised: "System.UnauthorizedAccessException" in System.Private.CoreLib.dll". Tried googling how to allow the application to access the file system, but in vain

private async Task HandleSaveClick()
{
    if (!string.IsNullOrEmpty(fileContent) && !string.IsNullOrEmpty(filePath))
    {
        var fileData = Encoding.UTF8.GetBytes(fileContent);
        using (FileStream stream = new FileStream(filePath, FileMode.Create))
        {
            await stream.WriteAsync(fileData, 0, fileData.Length);
        }
    }
}

2 Answers2

4

MAUI's developer team recently launched the official file storage API, which you could refer to FileSaver for more details.

Alec - MSFT
  • 121
  • 4
0

Can you show the details about the file path? This error means the file path you are trying to access is denied by the system. I have created a sample to save the file in the Folder metioned in the official document about the Maui File System Helper.

using (FileStream filestream = new FileStream(Path.Combine(FileSystem.CacheDirectory, "Test.txt"), FileMode.Create))
{
    var data = Encoding.UTF8.GetBytes("this is content");
    await filestream.WriteAsync(data,0,data.Length);
}

And the file will be saved into the FileSystem.CacheDirectory.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14