0

I write some data to a file that the user can choose with a File Picker. Here my test code with some nonsense data:

private async void DoExportAsync()
{
    // write some data to a temporary file
    string tempPath = Path.Combine(FileSystem.CacheDirectory, Guid.NewGuid().ToString() + ".tmp");
    try
    {
        using (var writer = new StreamWriter(tempPath))
        {
            writer.WriteLine("Test");
            writer.Flush();
        }

        // Now copy content to the final file that the user selects
        string? finalPath = await FileUtils.SaveFileAsync(tempPath, "PackerData", ".txt");
        // Display a message on success
        if (finalPath != null)
            await App.Current.Windows[0].Page!.DisplayAlert("Export", $"Gespeichert unter:\n{finalPath}", "OK");
    }
    finally
    {
        // delete the temporary file
        if (File.Exists(tempPath))
            File.Delete(tempPath);
    }
}

FileUtils.SaveFileAsync calls

await CommunityToolkit.Maui.Storage.FileSaver.Default.SaveAsync(...)

When I am debugging this code, the debugger detaches when the using { } block is left. After that the file picker still appears and writes the file, but the file is empty. My app disappears from the screen. DisplayAlert is not executed.

However, this code works well in the Android Emulator when no debugger is attached. (I did not yet test the behaviour on a real smartphone.)

Any idea how to work around this problem?

1
  • This seems to be a synchronsation issue. When I write await using (StreamWriter writer = new StreamWriter(tempPath)) the debugger detaches later, at await CommunityToolkit.Maui.Storage.FileSaver.Default.SaveAsync(...) Commented Oct 16 at 14:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.