Search code examples
c#.netmonoportable-class-library

How does one create a directory with a PCL


While dealing with creating a Portable Class Library out of current code for a project, some workarounds are fairly obvious and some are problematic.

System.IO.Directory is non-PCL and I still need to be able to create a directory before creating files inside them.

How do you create a folder in C# without being able to call Directory.CreateDirectory(..)?


Solution

  • There is no built-in file and directory I/O support in PCL, since this functionality differs from platform to platform. However, to circumvent this issue you could reference PCLStorage in your portable class library project.

    PCLStorage provides a portable abstraction layer library for file and directory I/O that you would reference in your portable class library. In your platform-specific application implementation, you would incorporate the corresponding implementation library of this abstraction layer.

    PCLStorage is applicable to .NET Framework 4 and higher, Silverlight 4 and higher, Windows Phone 7.5 and higher, and Windows Store apps. It relies on async and await, which means that it is dependent on the BCL Async package when used e.g. with .NET 4, Silverlight and Windows Phone 7.5.

    You might also want to have a look at the MvvmCross File plug-in. MvvmCross is portable "by nature" and the File plug-in provides relevant file and directory I/O functionality as synchronous methods. MvvmCross portable libraries are currently applicable to .NET Framework 4.5, Silverlight 4 and higher, Windows Phone 7.5 and higher, Windows Store apps, Xamarin.iOS and Xamarin.Android.