locked
How to use System.IO.File or similar for cross platform projects in PCLs? RRS feed

  • Question

  • User18034 posted

    I have setup my project lib as .NET 4.5 for Windows Phone 8, Android and iOS. Unfortunately System.IO.File is not available in this combination. If I unstick "Windows Phone 8" and only leave Android and iOS, XS automatically adds "Silverlight" and the solution does not build anymore. So I'm looking for an alternative to System.IO.File. Any ideas?

    Monday, September 2, 2013 2:37 PM

Answers

  • User1799 posted

    According to the MSDN Documentation, System.IO.File is not supported in PCL.

    Depending on which methods you need, you can easily work around by using FileStream - look at Mono's source code to see how it's implemented there: https://github.com/mono/mono/blob/master/mcs/class/corlib/System.IO/File.cs

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, September 4, 2013 9:54 PM

All replies

  • User3932 posted

    I use streams: in my main PCL project I use a IoC construction to provide a Asset and File helper.

    Like: public interface Stream getAssetStream(String name); Stream getFileStream(String name);

    And in the platform-project, I push a class to the IoC-service that implements this interface. That way only the file reference itself is platform-dependend.

    Of course you can also provide this class in the constructor of the PCL-class.

    Hope this gets you in the right direction.

    Tuesday, September 3, 2013 10:50 AM
  • User1799 posted

    According to the MSDN Documentation, System.IO.File is not supported in PCL.

    Depending on which methods you need, you can easily work around by using FileStream - look at Mono's source code to see how it's implemented there: https://github.com/mono/mono/blob/master/mcs/class/corlib/System.IO/File.cs

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, September 4, 2013 9:54 PM
  • User131074 posted

    Apologies to bring to life an old thread, but...

    I'm just starting out with Xamarin development and I've noticed a reoccurring theme over the past few days. The reason most developers want to use Xamarin is because of the cross-platform capabilities. Most developers want to create a PCL that does 3 main things:

    1) Load JSON or XML data from a REST API or other type of HTTP resource. 2) Connect to a database through Sqlite. 3) Do some basic file system operations (required for no. 2 above).

    After Googling for the past few days to try accomplish the above tasks I've had to wade myself through a forrest of out of date or incomplete examples on the Xaramin website (not to mention the missing "using" statements in the code samples which makes for very frustrating "where the hell is this thing supposed come from?" kind of moments). I've been confronted by what seems like multiple implementation that are doing the same thing (if not the exact same thing by implementing common interfaces), good examples are the plethora of JSON and Sqlite implementations.

    The file system access which is the main topic of this thread seems to be another good example. Sure, use streams. Martin points to a source which seems to an implementation of "System.IO.File". Are other developers supposed to copy this into their own project? As Hugo is pointing out, do I have to write my own abstractions (interfaces) to accomplish this.

    It seems that every developer touching Xamarin is writing their own abstractions for these things. Why isn't there a "Xamarin" namespace (e.g.) that provides good solutions for all these common problems? It seems that we're as developers all are reinventing the wheel, which is a shame.

    As I said, I'm just starting out with Xamarin and I'm new to the .NET/Mono/C# ecosystem but I'm a pretty experienced developer (20+ years). I should be able to figure these things out pretty quickly but so far it has been quite a frustrating encounter of obfuscations and abstractions. The NuGet dependency manager doesn't really make things easier either, which package to choose?

    Sorry for the rant..

    Thursday, June 4, 2015 2:01 AM
  • User31385 posted

    If you can grok it, the Akavache project https://github.com/akavache/Akavache promises to get rid of the need to do both 2 and 3 directly, by giving you an abstract caching layer that accepts functions which fetch its data if they aren't in the cache or if they have expired. It's not the easiest thing to get your head around, but given your level of experience you may get on better than most. (Under the hood, of course, it does do 2 and 3 internally, but it provides an interface that means you won't have to).

    Thursday, June 4, 2015 7:25 AM
  • User94 posted

    Rookie, I know how you feel, I have lost the last couple of days trying without success to get REST/JSON to work in a PCL, I am about to give up and go back to my tried and trusted way of communication of using sockets, this is my second attempt at getting REST/JSON to work, I have not found a single example that worked without significant modification just to compile. Communication works just cannot deserialise the objects, a complete mess IMHO, a PCL example of CRUD operations from Xamarin on what they recommend would have saved me a couple of days of banging my hard against a brick wall.

    I am not even trying something complex at the moment an object that has a single string field does not work.

    atb

    alex

    Thursday, June 4, 2015 4:51 PM
  • User42871 posted

    Hi @Rookie, @AlexWhite,

    This kind of problem has become a lot easier since this thread was originally posted. These days an approach referred to as the 'bait-and-switch' pattern is widely used and has resulted in the development of a bunch of NuGet 'plugins' that allow you to access differing native APIs and functionality within your PCL. This is a good and maintained catalog of PCL-friendly abstractions for various functionalities, all just require you to install the NuGet package in all projects (that is, the PCL project, plus the platform ones).

    For the specific items mentioned in the thread: - For REST+JSON - Refit - no fuss, PCL friendly, async friendly. - For JSON, JSON.NET of course has you covered - no fuss, PCL friendly. - If you really truly want to write to the filesystem, PCLStorage - PCL friendly. - If you just want to persist data, I also recommend Akavache, as suggested by @DavidDancy. it is also no fuss, PCL friendly. - SQLite - Mono.Data.SQLite

    Hope that helps you get started.

    Sunday, June 7, 2015 10:35 AM
  • User131074 posted

    Thanks Ryan. Looking into it.

    Sunday, June 7, 2015 10:40 AM
  • User94 posted

    Thank Ryan, have been getting some success since I posted, one of the things I was missing was as you say installing the packages into the PCL and the platform projects, it still would be lovely if there was an end-to-end example of a cross platform app using these as this is quite a common requirement.

    Sunday, June 7, 2015 12:02 PM
  • User320617 posted

    @RyanDavis said: Hi @Rookie, @AlexWhite,

    This kind of problem has become a lot easier since this thread was originally posted. These days an approach referred to as the 'bait-and-switch' pattern is widely used and has resulted in the development of a bunch of NuGet 'plugins' that allow you to access differing native APIs and functionality within your PCL. This is a good and maintained catalog of PCL-friendly abstractions for various functionalities, all just require you to install the NuGet package in all projects (that is, the PCL project, plus the platform ones).

    For the specific items mentioned in the thread: - For REST+JSON - Refit - no fuss, PCL friendly, async friendly. - For JSON, JSON.NET of course has you covered - no fuss, PCL friendly. - If you really truly want to write to the filesystem, PCLStorage - PCL friendly. - If you just want to persist data, I also recommend Akavache, as suggested by @DavidDancy. it is also no fuss, PCL friendly. - SQLite - Mono.Data.SQLite

    Hope that helps you get started.

    In PCLStorage, I couldn't see FileStream... How can I use FileStream in Xamarin Forms?

    Thursday, August 24, 2017 9:28 AM
  • User275526 posted

    Dear All,

    I want to compare file about new or old version between personal folder and assets folder in PCL project.

    Please advice me.

    Sunday, September 3, 2017 2:41 PM
  • User366904 posted

    I would like to get the size of a file to perform multipart uploading using AWS. How can I do this if 'System.IO.File' is not available?

    Thursday, April 5, 2018 10:02 AM