简体   繁体   English

如何在 .NET MAUI 中保存临时文件?

[英]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?如何在 Windows、IOS 和 ZE84E30B9390CDB64DB6ZDB2CA9D 上运行的 .NET MAUI 应用程序中存储临时文件

And how to retrieve its path?以及如何检索它的路径?

You can use File Handling .您可以使用文件处理

For more information about it, you can refer to File Handling in Xamarin.Forms有关它的更多信息,您可以参考Xamarin.Forms 中的文件处理

You can use this tool to save the data in a file.您可以使用此工具将数据保存在文件中。 It is also effective in .NET MAUI projects.在 .NET MAUI 项目中也有效。

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

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.使用 .NET Maui 管理临时文件的最佳方法是使用 Microsoft.Maui.Storage 命名空间中的内置文件系统帮助程序。 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.对于临时文件,您可能需要考虑使用 FileSystem.CacheDirectory 返回的路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM