2

I'm running into a snag utilizing the StreamReader class. On the StreamReader Class documentation page, it states that it supports Universal Windows Platforms (UWPs) under the Version Information header, "Universal Windows Platform - Available since 8".

Upon further inspection of its constructors, the StreamReader(Stream) constructors do support UWP apps, however the StreamReader(String) constructors do not support them.

I'm currently using the StreamReader(String) constructor with the complete file path to to be read,

using (StreamReader sr = new StreamReader(path))
{
    ...
}

I'm seeking to learn how to convert my code for a StreamReader(String) to a StreamReader(Stream).

jtth
  • 876
  • 1
  • 12
  • 40
  • Is [this](https://stackoverflow.com/questions/1879395/how-to-generate-a-stream-from-a-string) what you want to accomplish? – Kilazur Jun 14 '17 at 15:43
  • Is you string a filename or a string. To read a string use StringReader(string). The string in a StreamReader is a filename. – jdweng Jun 14 '17 at 15:50
  • @Kilazur, in a sense yes. I want to use the path. – jtth Jun 14 '17 at 16:51
  • @jdweng, right but if you read the question carefully it specifies that StreamReader(string) is not supported by UWPs. Thus I was looking for an alternative solution. – jtth Jun 14 '17 at 16:52

2 Answers2

2

In UWP StreamReader accepts only Stream with additional Options. Not String.

So to use StreamReader from a particular path, you need to get the StorageFile

StorageFile file = await StorageFile.GetFileFromPathAsync(<Your path>);
var randomAccessStream = await file.OpenReadAsync();
Stream stream = randomAccessStream.AsStreamForRead();
StreamReader str = new StreamReader(stream);
AVK
  • 3,893
  • 1
  • 22
  • 31
0

Ended up solving my own problem! The documentation is spot on.

From

using (StreamReader sr = new StreamReader(path))
{
    ...
}

To

using (FileStream fs = new FileStream(path, FileMode.Open))
{
   using (StreamReader sr = new StreamReader(fs))
   {
      ...
   }
}

Simple and elegant. Thanks again to all who contributed!

jtth
  • 876
  • 1
  • 12
  • 40
  • You'll get an ACCESS_DENIED error if the path is outside of the app folders. – Mehrzad Chehraz Jun 14 '17 at 17:56
  • 1
    Welcome to the [sandboxed](https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions) environment! You need to get an instance of [StorageFile](https://learn.microsoft.com/en-us/uwp/api/windows.storage.storagefile), usually through a [FileOpenPicker](https://learn.microsoft.com/en-us/uwp/api/windows.storage.pickers.fileopenpicker), then use the method in AVK's answer. You cannot access files directly using a path. – Mehrzad Chehraz Jun 14 '17 at 18:09
  • @MehrzadChehraz I believe that is exactly what is happening to me... Is there a workaround or known solution? I need to be able to access files outside of the builds app folders as its a dynamic program that will work for any user. Would any of [FileStreams params](https://msdn.microsoft.com/en-us/library/system.io.filestream(v=vs.110).aspx) work here? – jtth Jun 14 '17 at 18:12
  • No, You need to use the StorageFile to get an stream to the file if it is outside of app folders. – Mehrzad Chehraz Jun 14 '17 at 18:18
  • @MehrzadChehraz "You need to get an instance of StorageFile, usually through a FileOpenPicker". The documentation on FileOpenPicker here is quite dense. Would you draft a prototype example here to illustrate how you mean? – jtth Jun 14 '17 at 18:41
  • [Here](https://code.msdn.microsoft.com/windowsapps/File-Picker-in-Windows-10-846c2116#content) you can find a sample. – Mehrzad Chehraz Jun 14 '17 at 19:13
  • Hmm.. well I seem to get errors from the await methods, and the script doesn't work without them either. – jtth Jun 15 '17 at 14:01