Why Do I Get a File Does Not Exist Error When I Use PCLStorage ?

DyaryDyary Member ✭✭

Hi. I am Creating a file using PClStorage then I retrieve the text from that file using another PCLStorage method and I assign it a property called MyStringFile. And I have a label with the text property binding to this MyStringFile property.

I get no squiggly line in my code but when I run the application it throws a FileNotFound exception.

Here is the .cs file I am using

using PCLStorage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace FileSystem
{

    public partial class MainPage : ContentPage
    {

        public string MyStringFile { get; set; }

        public MainPage()
        {
            InitializeComponent();
            BindingContext = this;

        }

        public async Task CreateDummyFile()
        {


                IFolder rootFolder = PCLStorage.FileSystem.Current.LocalStorage;
                IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
                    CreationCollisionOption.OpenIfExists);
                IFile file = await folder.CreateFileAsync("answer.txt",
                    CreationCollisionOption.ReplaceExisting);
                await file.WriteAllTextAsync("42");


        }

        protected async override void OnAppearing()
        {

            await CreateDummyFile();

            var textFile =   await PCLStorage.FileSystem.Current.LocalStorage.GetFileAsync("answer.txt");

            MyStringFile = await textFile.ReadAllTextAsync();

            base.OnAppearing();


        }

    }
}

And here is the XAML file

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:FileSystem"
             x:Class="FileSystem.MainPage">

    <StackLayout>

        <Label  x:Name="lable1"  Text="{Binding MyStringFile}"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
    </StackLayout>

</ContentPage>

How do I resolve this issue ?
Thanks .

Answers

  • BushbertBushbert Member ✭✭✭
    edited August 2018

    Looks like your creating it in the MySubFodler but not retrieving it from there.

    var textFile = await PCLStorage.FileSystem.Current.LocalStorage.GetFileAsync("MySubFolder\answer.txt");

  • DyaryDyary Member ✭✭
    edited August 2018

    Thanks a lot Bushbert. I used what you suggested but still get the exception. when I debug it I get the exception when the CreateDummyFile execute at this line

    IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
                        CreationCollisionOption.OpenIfExists);
    

    it Never creates the folder in the first place.

  • BushbertBushbert Member ✭✭✭

    Can you share the exception details?

  • ColeXColeX Member, Xamarin Team Xamurai

    @Dyary said:
    Thanks a lot Bushbert. I used what you suggested but still get the exception. when I debug it I get the exception when the CreateDummyFile execute at this line

    IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
                        CreationCollisionOption.OpenIfExists);
    

    it Never creates the folder in the first place.

    Same code works fine on my side .

    Check the Installation.

    If you reference the package from a Portable Class Library, you will also need to reference the package from each platform-specific app. This is because the Portable Class Library version of PCL Storage doesn't contain the actual implementation of the storage APIs (because it differs from platform to platform), so referencing the package from an app will ensure that the platform-specific version of PCL Storage is included in the app and used at runtime.

    Did you reference the package from each platform project?

Sign In or Register to comment.