Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StreamReader and Portable Class Library

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?
like image 759
Anthony Nichols Avatar asked Aug 07 '13 17:08

Anthony Nichols


2 Answers

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:

  • How to Make Portable Class Libraries Work for You
  • Portable Class Library Enlightenment / Adaptation
  • Using Target-Specific Code in a Portable Library
like image 64
Daniel Plaisted Avatar answered Sep 28 '22 20:09

Daniel Plaisted


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.

like image 20
Anthony Nichols Avatar answered Sep 28 '22 22:09

Anthony Nichols