Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Xamarin System.IO Read/Write file not persisting between sessions

$
0
0

I am unable to write and then read files between sessions. Within a session everything works as expected, however, when I close the app and reload it I receive a "FileNotFoundException" when I do my initial read on application start.

Here are some facts
- Initially developing on Android but want iOS support
- I have External Read/Write permission
- I have tried Environment.SpecialFolder.Personal
- I have tried Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath

Here is my code

    void SaveText(string text)
    {
        var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Global");

        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        var filePath = Path.Combine(path, SessionFileName);
        File.WriteAllText(filePath, text, Encoding.UTF8);
    }

    string LoadText()
    {
        var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Global");

        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        var filePath = Path.Combine(path, SessionFileName);
        return File.ReadAllText(filePath, Encoding.UTF8);
    }

Perhaps correlated, App.Current.Properties has the same issue.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>