How to correctly save and read files

I've been trying to save a file in order to store the user's username in so that they don't have to enter it every single time they want to use the app. However, I haven't found much information on how to actually save and read a file, so could someone point me in the right direction?


  • betimbetim ALMember, University ✭✭

    Did you considered settings.

    I think for this scenario are a very good approach.

  • JamesMongerJamesMonger USMember

    Did you considered settings.
    I think for this scenario are a very good approach.

    Could you give me a link to some documentation about how to use settings?

  • JamesMongerJamesMonger USMember
    edited August 2013

    @SKall, I attempted to run that and it worked fine but for some reason it didn't actually save the file - I ran it once and executed the code to write the file, then I closed the program, opened it again and then executed the code to read the file, and it failed to read it.

    EDIT: this was on Android by the way

  • NickTalbotNickTalbot USMember

    What device are you running on? I found that running on a Nexus 7 saving to the Environment.GetFolderPath(Environment.SpecialFolder.Personal) directory never worked.

    I changed the directory to Environment.ExternalStorageDirectory and it then worked fine.

    As stated in

    "Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer."

    I.e. It's not 'External' on a Next 7 with no SD card - but it is stored on the devices 'internal 16GB memory'

  • SKallSKall USMember ✭✭✭✭

    Funny, on my Nexus 7 it works. One should probably look into isolated storage as well.

  • JamesMongerJamesMonger USMember

    I'm running on a Samsung Galaxy SII GT-I9100 (physical device)

  • DWestyDWesty USMember ✭✭
    edited August 2013

    Shared prefs are probably the right place for Username and even password info. Just remember any time you store a password don't save it as plain text...

    Also you can always open a private stream... OpenFileOutput(string filename,FileMode) which opens a file which is private to your application.

  • CarloOrsiCarloOrsi ITMember
    edited April 2014

    Hi everybody, same JamesMonger's problem here. The Android device used to debug the app seems not persist the txt file saved on Environment.SpecialFolder.Personal path. It works fine until I turn off the phone, when I restart it the stream read doesn't see any txt file, as if it had disappeared. Really strange.
    @JamesMonger‌ have you find a solution? Or did you use ExternalStorage (that it works for me, but is too easily accessible to the user).
    I've also try to see the file list on path using adb shell, but the result once inside the dir is "permission denied".

  • wendaowendao USMember

    the code you attached ` public static string GetString(this Application application, string key)
    var prefs = application.Context.GetSharedPreferences(application.PackageName, FileCreationMode.Private);
    return prefs.GetString(key, string.Empty);
    public static void SaveString(this Application application, string key, string value)


    var prefs = application.Context.GetSharedPreferences(application.PackageName, FileCreationMode.Private);
    var prefEditor = prefs.Edit();
    prefEditor.PutString(key, value);


  • BlackShawarnaBlackShawarna ITUniversity ✭✭

    Anyone knows why when i'm using this code, in runtime tha path start with // ??

    using( StreamReader reader = new StreamReader( @"file://" + _path + "/" + _chap.Name) ) { string source = reader.ReadToEnd (); web_view.LoadDataWithBaseURL(null, source, "text/html", "UTF-8", "about:blank"); }

    In runtime my path is ://file:///data....../test.html.

  • RaymondKellyRaymondKelly USMember ✭✭✭

    I have having the exact same problem as CarloOrsi. After I create my file, it works fine until I close my app and reopen it. The file just disappears. Does anyone know whats going on?

            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string settingsPath = Path.Combine(path, "settings.xml");
            StreamWriter stream = File.CreateText(settingsPath);
  • RaymondKellyRaymondKelly USMember ✭✭✭

    And now I just found the answer. See this post for disappearing files. "Preserve application data/cache on device between deploys"

  • Hi. I'm having the same problem, but with a little extra. I want the files, saved by the application, available to be used by another programme (excel in this case). So I will save the file as a .CSV, but where will I find it? Or how can I direct the programme to save it in a specific directory?

  • Rajat.4176Rajat.4176 USMember

    In my samsung tab4 i have an external micro sd card. This is identified by the device as extSdCard. Now I need to add a folder in this. But I am getting "Access to this path is denied". Any solution???

  • SamirgcSamirgc USMember ✭✭

    Please follow along to get the solution for xamarin forms :smile:

Sign In or Register to comment.