Home .NET File System Access from the Portable Class Library (PCL)

File System Access from the Portable Class Library (PCL)

by admin

One day I had the idea of porting my .NET code to other platforms, putting all the core logic (everything that doesn’t concern the UI) into a separate library – the Portable Class Library (PCL).
It sounds cool in theory – you can work with PCL on almost all devices, not only with .NET Framework, but even with Mono! This approach opens up great multiplatform perspectives, for example, porting Windows Phone code to Android and iOS using Xamarin.
File System Access from the Portable Class Library (PCL)
But after getting acquainted with PCL, I came to a conclusion that such a library is in fact a stripped-down .NET, taking into account the limitations of each platform. That is, even on seemingly similar WP8 and W8 you can’t shove everything into PCL.
File System Access from the Portable Class Library (PCL)
Table of PCL capabilities on different platforms
At first glance my code should have passed without any problems (target platforms : Windows .NET, Windows 8, Windows Phone), but the compiler cussed System.IO, namely StreamReader, which directly opened the file.
Remembering that Windows 8 and Windows Phone have completely different ways of working with files and different from each other :
WP uses IsolatedStorage to access the application files, while Windows 8, though it has access to public folders, does so via Windows.Storage (WP8 can now do it too, but this method is not recommended to maintain WP7 support). After googling it became clear to me that organizing access to the files on pure PCL is impossible, and tips like transferring Steam already from the application or hard inclusion of the file into DLL didn’t fit my solution. The thing is that the program worked with a large external dictionary and stored the temporary results in the files. But here we saw the PCLStorage library, which promised some sort of work with the FS via PCL.
PCLStorage provides three interfaces – IFIle, IDirectory, IFileSystem, all of them provide asynchronous operations of reading and writing files and directories.
Note : All further tests were done on "classic" Windows.
Example of writing a file :

public async Task PCLCreateFile(string name, string content){IFolder localStorage = FileSystem.Current.LocalStorage;IFolder contentFolder = await localStorage.CreateFolderAsync("Content", CreationCollisionOption.OpenIfExists);IFile file = await contentFolder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);await file.WriteAllTextAsync(content);}

Example of reading a file :

public async Task<string> PCLReadFile(string name){IFolder localStorage = FileSystem.Current.LocalStorage;IFolder contentFolder = await localStorage.GetFolderAsync("Content");IFile file = await contentFolder.GetFileAsync(name);return await file.ReadAllTextAsync();}

It looks and works fine. But… how do I access a file that is in the program directory?
IFileSystem returns two paths LocalStorage and RoamingStorage. The first one points to the directory which is allocated for the program in the current system, the second one points to the public directory for data synchronization. In particular, on Windows, LocalStorage pointed to (AppDataLocal), which was far from being an application directory and didn’t contain any files.
The PCLStorage application directory, unfortunately, could not be pointed to.
Of course, it was possible to create a special installer, but what about in the case of Store applications? Nevertheless, a solution appeared – copying files to LocalStorage from the application itself (or downloading from the network in PCL itself (not checked)). Well, the way ended up being simpler than Stream transfer, plus it was possible to work with temporary files in a limited portable library environment.
P.S.: In the end of the experiment, it turned out that WP7 support was dropped from the latest version. That was actually the reason (IsolatedStorage on WP and Windows.Storage on Windows 8 were used separately).

You may also like