Saving a "permanent" file in .NET MAUI

Viewed 53

I am generating a file in a .NET MAUI application, and I want to save it to the Files folder. I followed the issue asking about how to save a temporary file in .NET MAUI, but I want my output to be "permanent" location (well, until the user deletes it, anyway).

I am using the following code:

  string FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myfile.gz");
  byte[] arr = GetDataToSave();
  await File.WriteAllBytesAsync(fileName, arr);

I'm running this code on an Android emulator, and I'm expecting to see a file created here:

Android Emulator

I'm looking for the file so I can drag it to my Windows machine and inspect the contents to make sure I created the file successfully. But, it's not showing up.

How can I save a file from .NET Maui in such a way that I can see it in Files on the Android Emulator?

1 Answers

From the following code , we can find that you are trying to save file to internal storage.

string FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myfile.gz");

And from document File Storage and Access with Xamarin.Android, we know that

Internal Storage – this is a portion of the file system that can be accessed only by the application or the operating system.

So, if you save file to internal storage, you cannot access this file by other apps. That's why you can't see this file with other apps.

Of course, if you want to see this file with other app, you can save your file to External Storage.

External Storage is a partition for the storage of files that is accessible by all apps, the user, and possibly other devices. On some devices, external storage may be removable (such as an SD card).

For more details, you can check document: External storage.

And there is also an sample included in above document, you can check it here: LocalFiles.

Related