This one is easy, but a bit tricky if you are just looking it up online. When working with a Universal App in Windows, you generally use the new Windows.Storage.StorageFile
class. This works great, except when you need to get a System.IO.Stream
instance.
If you do a quick google search, the simple way is to use the following method:
var stream = await file.OpenStreamForReadAsync();
This makes creating the stream nice and easy, but you probably got an error:
'Windows.Storage.StorageFile' does not contain a definition for 'OpenStreamForReadAsync' and no extension method 'OpenStreamForReadAsync' accepting a first argument of type 'Windows.Storage.StorageFile' could be found (are you missing a using directive or an assembly reference?)
Fortunately, there is an easy fix, once you realize the extension method OpenStreamForReadAsync
is actually located in the System.IO
namespace, and not the Windows.Storage
namespace where you might expect it. To fix it, all you need to do is add the following statement at the top of your file:
using System.IO;
You can now easily create your Stream
straight from a StorageFile
instance.