title: Xamarin PCLStorage Isn't Writing/Reading A Text File
body: <p>I'm working on a Xamarin.Forms (I guess the Forms part is irrelevant) app and trying to use the PCLStorage Xamarin plugin to save a JSON string to a text file.  The text file will be there for cached data in case an internet connection isn't available.</p>
  
  <p>I added the plugin to all projects (PCL, iOS, Android, UWP).  I added the following code to my portable class.</p>
  
  <pre><code>using PCLStorage;
  
  namespace DailyBibleReading
  {
      public class Helper
      {
          // read a text file from the app's local folder
          public static async Task&lt;string&gt; ReadTextFileAsync(string _filename)
          {
              // declare an empty variable to be filled later
              string result = null;
  
              // see if the file exists
              try
              {
                  // get hold of the file system
                  IFolder rootFolder = FileSystem.Current.LocalStorage;
  
                  // create a folder, if one does not exist already
                  //IFolder folder = await rootFolder.CreateFolderAsync("DailyBibleReading", CreationCollisionOption.OpenIfExists);
  
                  // create a file, overwriting any existing file
                  IFile file = await rootFolder.CreateFileAsync(_filename, CreationCollisionOption.ReplaceExisting);
  
                  // populate the file with some text
                  result = await file.ReadAllTextAsync();
              }
              // if the file doesn't exist
              catch (Exception ex)
              {
                  // Output to debugger
                  Debug.WriteLine(ex);
              }
  
              // return the contents of the file
              return result;
          }
  
          // write a text file to the app's local folder
          public static async Task&lt;string&gt; WriteTextFileAsync(string _filename, string _content)
          {
              // declare an empty variable to be filled later
              string result = null;
              try
              {
                  // get hold of the file system
                  IFolder rootFolder = FileSystem.Current.LocalStorage;
  
                  // create a folder, if one does not exist already
                  //IFolder folder = await rootFolder.CreateFolderAsync("DailyBibleReading", CreationCollisionOption.OpenIfExists);
  
                  // create a file, overwriting any existing file
                  IFile file = await rootFolder.CreateFileAsync(_filename, CreationCollisionOption.ReplaceExisting);
  
                  // populate the file with some text
                  await file.WriteAllTextAsync(_content);
  
                  result = _content;
              }
              // if there was a problem
              catch (Exception ex)
              {
                  // Output to debugger
                  Debug.WriteLine(ex);
              }
  
              // return the contents of the file
              return result;
          }
      }
  }
  </code></pre>
  
  <p>You might notice that the <code>CreateFolderAsync</code> sections are commented out.  I didn't feel the need to create another folder inside the app's folder.  I did try it both ways, though, just in case it was a requirement.  Neither way worked.  You might also notice that <code>LocalStorage</code> is in use.  I also tried <code>RemoteStorage</code>, with the same results.</p>
  
  <p>I make calls to those methods when I get the JSON string.</p>
  
  <pre><code>string result = null;
  
  // make the api call
  result = await Helper.GetHttpStringAsync("http://www.anti-exe.com/api/dailybiblereading?begindate=" + _begindate + "&amp;enddate=" + _enddate + "&amp;version=" + _version);
  
  string textfile = "ApiResults.txt";
  // if there is no content
  if (result == null || result == "" || result.Contains("SQLSTATE") == true)
  {
      // read content from a pre-existing file
      result = await Helper.ReadTextFileAsync(textfile);
      // Output to debugger
      Debug.WriteLine("Can't connect to the API." + "\r\n" + "Loading from local cache.");
  }
  else
  {
      // write to a cache file
      await Helper.WriteTextFileAsync(textfile, result);
  }
  </code></pre>
  
  <p>It all seems pretty straight-forward (not to mention that it works, just fine, with my native Windows 10 code).  Try and get the JSON.  If nothing is there (no internet or API is down), grab the JSON string from the cache.  If JSON is there, write it to the cache.</p>
  
  <p>My assumption is that it isn't actually writing to the file.  I don't know any way of actually checking the contents of that file (can't just open Notepad and load it).</p>
  
  <p>I open the app.  I close the app.  I remove the internet access.  I open the app.  When it's all done, <code>result</code> is empty (<code>result == ""</code>).</p>