Xamarin Android file system paths

11,705

I think this is what you need:

Edit

public static string Directorypath
    {
        get
        {
            return System.IO.Path.Combine((string)Android.OS.Environment.ExternalStorageDirectory, "FolderPath");
        }
    }

enter image description here

Share:
11,705
Aidal
Author by

Aidal

Updated on June 20, 2022

Comments

  • Aidal
    Aidal almost 2 years

    I'm trying to write and read a file on the Android file system. A task you would think would be straight forward, but I just can't seem to find the right enumerator for a folder/path that actually exists.

    I have read several posts on the same subject but none of them give a straight answer to this question and those that seem to have fixed their issues, has done so with paths that doesn't seem to work for me. Some suggestions seem to be related to System.IO while others seem to be of the Java equivalent.

    What I'm trying to do is something as simple as to create a file somewhere in the internal storage (NOT external SD card or something) that is accessible from the device itself (via File Browser app or similar).

    If that goes well (which it currently doesn't) then I would also like to read it later on.

    I'm not looking for code to write or read files - I'm looking for something that can give me the correct path to a folder where I can do this.

    I have tried many different variants, some of which are:

    Environment.GetFolderPath (Environment.SpecialFolder.Personal);

    returns the path: /data/data/my_app_full_name/files/

    global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath.ToString();

    returns the path: /storage/emulated/0/

    Can anyone give a clear answer as to which enumerator to use for this? - would be much appreciated.

    PS. I'm launching the app on an actual device (Android 5.0.2) from Xamarin Studio via USB.

    EDIT: The answer, for me at least, is found under the comments for replay by misho.

  • Aidal
    Aidal about 8 years
    Android.OS.Environment.GetExternalStoragePublicDirectory(And‌​roid.OS.Environment.‌​ExternalStorageDirec‌​tory) doesn't work because GetExternalStoragePublicDirectory() takes a string as argument. So I tried Android.OS.Environment.GetExternalStoragePublicDirectory(And‌​roid.OS.Environment.‌​ExternalStorageDirec‌​tory.AbsolutePath)) instead which gives me: /storage/emulated/0/storage/emulated/0 so also doesn't work.
  • arsena
    arsena about 8 years
    ups, sorry, copied wrong code. updated answer, check it now
  • Aidal
    Aidal about 8 years
    Path.Combine(Android.OS.Environment.ExternalStorageDirectory‌​.ToString(), "Download") returns: "/storage/emulated/0/Download" BUT, if I use the on-device file browser and go to the folder Download, the path shown on top is in fact "/storage/emulated/0/Download". Why it has this odd actual path I cannot explain - the word "emulated" confuses for sure, because it makes you think it has to do with the emulator in some way, although this is one the device... So for now I'm gonna try a bit using this path and see how things go - thanks misho - I'm gonna test some more.
  • arsena
    arsena about 8 years
    I'll try to explain that: if i remember correctly emulated stands for external storage, not the device itself. 0 defines user. there was Multi-User feature added from JellyBean 4.2 so if you add another user there will be another folder /storage/emulated/1/ and so on. To make it clearer: /storage/ - actual storage. /emulated/ - storage type (or smth, definitely not android emulator). /0/ - user. definition of emulated may not be 100% true but I'm pretty sure it has nothing to do with genymotion or any emulator.
  • arsena
    arsena about 8 years
    that's how above screen looks from android device monitor: i.imgur.com/Jo0lJLs.png my device is galaxy note 4. you can find device monitor in /android-sdk/tools/ folder
  • Aidal
    Aidal about 8 years
    If by external you mean SC-card then it makes no sense, because this phone has no SD-card inserted. I really would like to target the device internal memory. Or did you mean something else by external?
  • arsena
    arsena about 8 years
    I meant storage type as i said below
  • arsena
    arsena about 8 years
    Ok, here it is: Emulated storage is provided by exposing a portion of internal storage through an emulation layer and has been available since Android 3.0. The "/storage/emulated/" folder does not really exist. It's what might be called a "symbolic link", or, in simpler terms, a reference to where the real data is stored.
  • arsena
    arsena about 8 years
    more: If your Android only has internal memory and not a physical internal storage separate from it, it creates a logical partition out of it and makes it work like internal storage. References: source.android.com/devices/storage superuser.com/questions/949659/… quora.com/…
  • Aidal
    Aidal about 8 years
    Aha! so the path /storage/emulated/0/ is actually quite ok. Thanks for explaining this misho!