11

I am writing a ConfigManager class using Portable Class Libraries. PCL supports StreamReader and StreamWriter classes that I want to use, but the PCL version of those classes do not support passing in a string during construction. PCL also does not support the reader.Close() and writer.Close(). Lastly it doesn't support the FileStream class.

So I am looking for an answer to any one of the following questions:

  1. How can I get the StreamReader and StreamWriter classes working in a PCL?
  2. How can I create a new stream using PCL?
  3. What other alternitives do I have to load and save files in a PCL?
Anthony Nichols
  • 1,586
  • 2
  • 25
  • 50
  • Does it support disposing the stream objects? Because that should close it just fine. – Lasse V. Karlsen Aug 07 '13 at 21:25
  • 2
    Instead of .Close() use .Dispose(), no there is no solution, because there isn't a problem...file system access must be abstracted Just code against interfaces. Check: [link]hthttps://pclstorage.codeplex.com [link](https://github.com/slodge/MvvmCross/tree/v3/Plugins/Cirrious/File) – Dan Aug 08 '13 at 03:43

2 Answers2

8

Use Dispose() instead of Close() (or just wrap everything in a using statement). We've hidden/removed Close() in Windows Store apps and newer PCLs, because it does the same thing and people would be confused about which one to call.

Consider using PCL Storage for cross platform file system access.

Here are some blog posts you may want to refer to for how to approach platform-specific functionality in PCLs:

patridge
  • 26,385
  • 18
  • 89
  • 135
Daniel Plaisted
  • 16,674
  • 4
  • 44
  • 56
  • PCLStorage is async which is often inconvinient. Especially it is sad to use async PCLStorage on .NET/Xamarin/WP8 where System.IO exists on all platforms. – Grigory Jun 05 '14 at 19:34
  • To whoever reading this answer that would use PCL storage. PCL Storage isn't maintained anymore, I would advice not using it on a production application. Prefer implementing your own storage solution using dependency injection – Tifa Apr 19 '17 at 15:11
2

found the answer over here (by Rob Caplan): http://social.msdn.microsoft.com/Forums/windowsapps/en-US/386eb3b2-e98e-4bbc-985f-fc143db6ee36/read-local-file-in-portable-library#386eb3b2-e98e-4bbc-985f-fc143db6ee36

File access cannot be done portably between Windows Store apps and Windows Phone 8 apps. You will have to use platform specific code, to open the file and acquire a stream. You can then pass the stream into the PCL.

Since both Windows Store apps and Windows Phone 8 apps use the essentially the same Windows (Phone) Runtime classes from Windows.Storage to open files you can share the code (but not the binary) by linking a code file between the two projects. See Share code with Add as Link .

See Maximize code reuse between Windows Phone 8 and Windows 8 for more techniques for sharing code.

If anyone has a solution other than this I would be interested to hear it; also wondering about the .Close() methods in the PCL.

Anthony Nichols
  • 1,586
  • 2
  • 25
  • 50