简体   繁体   中英

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?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM