Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read text file with PCLStorage in Xamarin.Forms

Is there a way to read an embedded JSON file in a PCL project with PCLStorage module? I looked everywhere but couldn't find a sample related to this matter.

EDIT: PCLStorage link: https://github.com/dsplaisted/pclstorage

like image 832
Élodie Petit Avatar asked Aug 27 '15 10:08

Élodie Petit


1 Answers

you need something like this:

public static async Task<string> ReadFileContent(string fileName, IFolder rootFolder)
{
  ExistenceCheckResult exist = await rootFolder.CheckExistsAsync(fileName);

  string text = null;
  if (exist == ExistenceCheckResult.FileExists)
  {
    IFile file = await rootFolder.GetFileAsync(fileName);
    text = await file.ReadAllTextAsync();
  }

  return text;
}

to use:

IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder myCoolFolder = await rootFolder.CreateFolderAsync("MyCoolForler", CreationCollisionOption.OpenIfExists);
string fileContent = await this.ReadFileContent("MyCoolFile.txt", myCoolFolder);
like image 146
Igor Avatar answered Oct 18 '22 05:10

Igor