2020.09.01.

Xamarin.Forms working with files

By bence960206

We need always to working with files. In this sample, we will discuss about how can I create files and load from files.

We have to first download a NuGet package: Xamarin.Forms.Portable.FileStoreAndLoad.

After it finished, we have to separate the created folders like this:

UWP -> Yourapplication.UWP

Droid -> Yourapplication.Droid

iOS -> Yourapplication.iOS

After that we can create files and load from files via Dependency service:

DependencyService.Get<IFileStoreAndLoad.IFileStoreAndLoad>().SaveText(“filename.txt”,”text”);

string loadedText = DependencyService.Get<IFileStoreAndLoad.IFileStoreAndLoad>().LoadText(“filename.txt”);

You should take this methods just in case into a try-catch block.

You can display the loaded text now in a Label:

Content = new StackLayout

{

Children = {

new Label { Text = loadedText }

}

};