If you’re a Xamarin Forms developer, you’ve likely used PCLStorage (or other Dependency Service) to interact with the target platform’s file system in the portable class library’s code. However, since November 2017, Xamarin.Forms now uses a .NET Standard 2.0 class library and PCLStorage is no longer supported.
This isn’t a problem because in .NET Core 2.0 you now have access to System.IO.File’s GetFolderPath and SpecialFolder methods (see System.IO.File in .NET Core 2.0 docs).
Thus, you can get a path to the app’s local folder (the one that the app can save to) without having to write a Dependency Service (which is what PCLStorage does) by using:
var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
IMPORTANT: Make sure you always use Path.Combine to create the file path because different platforms use different path separators
var filePath = Path.Combine(LocalFolder, "notes.txt");
With a reference to the file path, you can now access the file. For example reading the text:
var notes = File.ReadAllText(filePath);
As a very simple example, I created a couple extension methods for Stream and Byte in the below FileExtensions class.
To test it, I created a ContentPage that downloads an image, saves it using the extension method and set a FileImageSource to confirm that it’s a file (instead of just using a StreamImageSource).
Note that the extension methods are very basic and shouldn’t be used in production as-is (i.e. no defensive programming code).
Here is the result at runtime on UWP: