Elie Tebchrani
Elie Tebchrani

Reputation: 349

How can I save temporary file in .NET MAUI?

How to store a temporary file in a .NET MAUI application which runs on Windows, IOS, and Android?

And how to retrieve its path?

Upvotes: 4

Views: 7970

Answers (2)

Greg Dietsche
Greg Dietsche

Reputation: 1050

The best way to manage temporary files using .NET Maui is to use the built-in file system helpers found in the Microsoft.Maui.Storage namespace. These helpers will give you a path that is valid on the device that your code is running on. For temporary files you may want to consider using the path that FileSystem.CacheDirectory returns.

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-system-helpers

Upvotes: 3

Wen xu Li
Wen xu Li

Reputation: 1686

You can use File Handling.

For more information about it, you can refer to File Handling in Xamarin.Forms

You can use this tool to save the data in a file. It is also effective in .NET MAUI projects.

Files are generated on all three platforms, and they can be extracted at any time.

I wrote an example for your reference. Here is the background code:

public partial class MainPage : ContentPage
{
    string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp.txt");//address
    public MainPage()
    {
        InitializeComponent();
    }
    private void mytest(object sender, EventArgs e)
    {
        File.WriteAllText(fileName, "aaaaazxczxcsaaaazxvcvmaaaaaaaa"); //save
    }
    private async void mycheck(object sender, EventArgs e)
    {
        await DisplayAlert("Alert", File.ReadAllText(fileName), "OK"); //read
    }
}

Upvotes: 0

Related Questions