Search code examples
mauimaui-blazormaui-windows

Saving files in MAUI


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);
        }
    }
}

Solution

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