I am trying to copy files from my app folder to the public Documents folder so that I can have access to it when needed. I am coding this for the Android OS. This is the code I have so far:
string sourceDirectoryPath = Path.Combine(FileSystem.Current.AppDataDirectory, "Logs");
if (!Directory.Exists(sourceDirectoryPath))
{
//No logs
return;
}
string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string destinationDirectoryPath = Path.Combine(documentsFolder, "Logs");
if (!Directory.Exists(destinationDirectoryPath))
{
Directory.CreateDirectory(destinationDirectoryPath);
}
foreach (string file in Directory.GetFiles(sourceDirectoryPath))
{
string destinationFile = Path.Combine(destinationDirectoryPath, Path.GetFileName(file));
File.Copy(file, destinationFile, overwrite: true);
}
I have tried using Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
, but it points to /data/user/0/MyApp/files/Documents
. How do I make documentsFolder
point to /storage/emulated/0/Documents
?
Of course, Environment.SpecialFolder.MyDocuments;
will return your App's Personal Documents folder.
For getting public documents folder path, try something like below.
string documentsPath = string.empty;
#if ANDROID
documentsPath = global::Android.OS.Environment.GetExternalStoragePublicDirectory(global::Android.OS.Environment.DirectoryDocuments)?.AbsolutePath;
#endif