Justin
Justin

Reputation: 6549

.NET MAUI writing file in android to folder that then can be accessed in windows file explorer

I am new to mobile development and am writing a .NET MAUI Android application and part of that application needs to be able to sync files. This device has no network connection and will be docked (connected to a PC) where files that were created on the device are fetched from the PC and then the PC will then load a new file to the device. This sync process will happen from a seperate desktop application.

Looking at the documentation on the File system helpers, I don't see a clearly documented way to write a file to a directory that is visible in File Explorer when I connect the device.

I am looking for a way to write to a directory that then is visible once docked. I don't want to open a dialog for a user to pick the save location as I want that location to always be consistent. Any ideas of where I can get a file path of a "public" folder to read and write files to?

Upvotes: 5

Views: 5738

Answers (2)

raphael
raphael

Reputation: 1

Use This

#if __ANDROID__

string visiblePath = Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDocuments).AbsoluteFile.Path.ToString();
         
#endif

Upvotes: 0

Justin
Justin

Reputation: 6549

Doing additional digging, this did the trick.

var docsDirectory = Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDocuments);

File.WriteAllText($"{docsDirectory.AbsoluteFile.Path}/atextfile.txt", "contents are here");

Upvotes: 10

Related Questions