2

I want to access a folder in which I can save temporary files and write there a file and where I don't need permission to write to the folder.

I currently use:

string targetBaseDir = Environment.GetFolderPath(
    Environment.SpecialFolder.Personal,
    Environment.SpecialFolderOption.Create
    );

This gives me a private directory for which I need no permissions which is the directory that corresponds to Context.getFilesDir(), however as I don't need the file to exist permanently it would be cleaner if I could save them in the directory that corresponds to Context.getFilesDir(). What do I have to write instead of .Personal to get the directory?

Christian
  • 25,249
  • 40
  • 134
  • 225
  • Check @[kjs566](https://stackoverflow.com/users/5479478/kjs566) 's answer [Sharing bitmap](https://stackoverflow.com/questions/33222918/sharing-bitmap-via-android-intent) Its for sharing for you can modify some code. – Morse Nov 08 '18 at 13:54

2 Answers2

2

You can use FileSystem.AppDataDirectory via Xamarin.Essentials in your .NetStd library to obtain what is the "getFilesDir()" location on Android.

Example:

var appData = Xamarin.Essentials.FileSystem.AppDataDirectory;

appData equals /data/user/0/com.sushihangover.FormsTestSuite/files

And the Android FilesDir:

Log.Debug("SO", FilesDir.ToString() );

Equals /data/user/0/com.sushihangover.FormsTestSuite/files

access a folder in which I can save temporary files

In that case use the Cache dir:

var cacheDir = Xamarin.Essentials.FileSystem.CacheDirectory;

cacheDir would equal: `/data/user/0/com.sushihangover.FormsTestSuite/cache

re: https://learn.microsoft.com/en-us/xamarin/essentials/

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
0

Both Environment.SpecialFolder.Personal and Environment.SpecialFolder.MyDocuments will give you the path of Context.getFilesDir().

However, if you want to save small temporary files, you can make use of the cache directory Context.getCacheDir().

There is no special folder enum for the cache directory though. You'll have to get the path from the current context Context.CacheDir.Path, or from Xamarin.Essentials.FileSystem.CacheDirectory if you use Xamarin.Essentials API.

Read more:
File access with Xamarin.Android with SpecialFolder
Writing cache file in internal storage
File system helpers in Xamarin.Essentials

Elvison
  • 163
  • 1
  • 8