Convert System.IO.Stream to a usable stream


  • We have a C# Windows runtime component that's used by our C++ app. One of the C# operations obtains a System.IO.Stream; we need to read data from this sequentially and then write it to a StorageFile.

    So, what are the steps to write the data from a System.IO.Stream to a StorageFile? We'd prefer to do this in the C++ app, but we could do it in the C# component if it's easier.

    Tuesday, April 17, 2012 12:18 PM

All replies

  • New to this whole WinRT thing myself, but I'm guessing that you can't pass a System.IO.Stream to a Metro C++ app because System.IO.Stream is managed, and C++/CX is native. You could probably write a wrapper around the Stream in C#, and make that .Net class interoperable with COM (which I have no idea how to do, but this might get you started.) If you can copy the entire stream to a byte array or something, that might work, but if that's not an option, you might be best off just doing it in C#.
    Tuesday, April 17, 2012 9:55 PM
  • It's possible to convert a WinRT IRandomAccessStream to a .Net System.IO.Stream using .Net's AsStreamForRead, so I thought it should be possible to go the other way--but I haven't been able to figure out exactly how.

    The problem is that we can't just write the System.IO.Stream using its own facilities because the user may have picked a file location that we can't access without the StorageFile object. We may end up having to read the .Net stream and copy it to a WinRT stream for writing, but that seems awfully inefficient.

    Tuesday, April 17, 2012 11:50 PM